λ¬Έμ
νμ΄ κ³Όμ
μ€ν
μ νμ©νλ λνμ μΈ κ΄νΈ μ λ§μΆκΈ° λ¬Έμ μ
λλ€. λ«λ κ΄νΈ
κ° λνλ¬μ λ μ€νμ κ°μ₯ μμ μ§μ΄ λ§λ μ¬λ κ΄νΈ
κ° μ‘΄μ¬νλμ§ νλ¨νλ©΄ λλ λ¬Έμ μ
λλ€.
μ΄λ² λ¬Έμ λ₯Ό ν΅ν΄ javascript
μ includes
λ©μλλ₯Ό νμ©νλ λ°©λ²μ μ μ μκ² λμμ΅λλ€. ||
μ°μ°μλ‘ or
μ²λ¦¬ ν΄μ£Όλ κ²μ΄ μ½λλ κΈΈμ΄μ§κ³ λ²κ±°λ‘μ λλ°
μνλ μμκ° μ‘΄μ¬νλμ§ νλ¨νλ includes
λ₯Ό νμ©ν΄μ μ½κ² 쑰건μ νλ¨ν μ μμμ΅λλ€.
μ½λ
/**
* @param {string} s
* @return {boolean}
*/
function top(stack) {
return stack[stack.length - 1];
}
var isValid = function (s) {
const bracket = [];
for (let c of s) {
if (["(", "{", "["].includes(c)) {
bracket.push(c);
} else {
if (!["()", "[]", "{}"].includes(top(bracket) + c)) {
return false;
}
bracket.pop();
}
}
return bracket.length === 0;
};
λ°μν
'π algorithm > leetcode' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
LeetCode 155 - Min Stack (Easy) (0) | 2021.03.02 |
---|---|
LeetCode 39 - Combination Sum (Medium) (0) | 2021.03.02 |
LeetCode 787 - Cheapest Flights Within K Stops (Medium) (0) | 2021.03.02 |
LeetCode 417 - Pacific Atlantic Water Flow (Medium) (0) | 2021.03.02 |
LeetCode 17 - Letter Combinations of a Phone Number (Medium) (0) | 2021.03.02 |
π¬ λκΈ