반응형
1. 문제
https://leetcode.com/problems/valid-parentheses/
2. 코드
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function(s) {
if(s.length % 2) return false;
let stack = [];
for(let i=0;i<s.length;i++){
if(s[i]==="(" || s[i]==="[" || s[i]==="{") stack.push(s[i]);
else if(s[i]==="}"){
if(stack[stack.length-1] === "{") stack.pop();
else return false;
}
else if(s[i]==="]"){
if(stack[stack.length-1] === "[") stack.pop();
else return false;
}
else if(s[i]===")"){
if(stack[stack.length-1] === "(") stack.pop();
else return false;
}
}
return stack.length ? false : true;
};
반응형
'리트코드 > easy' 카테고리의 다른 글
[리트코드] 205. Isomorphic Strings - js (0) | 2023.09.02 |
---|---|
[리트코드] 121. Best Time to Buy and Sell Stock - js (0) | 2023.08.29 |
[리트코드] 69. Sqrt(x) - js (0) | 2023.08.21 |
[리트코드] 509. Fibonacci Number - js (0) | 2023.08.20 |
[리트코드] 13. Roman to Integer - js (0) | 2023.08.19 |