728x90
https://leetcode.com/problems/valid-parentheses/
STL Stack을 사용하면 쉽게 풀리는~ 문제~
class Solution {
public:
bool isValid(string s) {
stack<char> st;
for(int i=0; i<s.size(); i++){
char c = s[i];
if(c == '(' || c == '{' || c == '['){
st.push(c);
}else if(c == ')'){
if(st.empty() || st.top() == '{' || st.top() == '[') return false;
if(st.top() == '(') st.pop();
else st.push(')');
}else if(c == ']'){
if(st.empty() || st.top() == '{' || st.top() == '(') return false;
if(st.top() == '[') st.pop();
else st.push(']');
}else if(c == '}'){
if(st.empty() || st.top() == '(' || st.top() == '[') return false;
if(st.top() == '{') st.pop();
else st.push('}');
}
}
if(!st.empty()) return false;
return true;
}
};
728x90
'알고리즘 > LeetCode' 카테고리의 다른 글
[LeetCode/easy] Length of Last Word (0) | 2022.08.12 |
---|---|
[LeetCode/easy] strStr() (0) | 2022.08.12 |
[LeetCode/easy] Longest Common Prefix (0) | 2022.04.18 |
[LeetCode/easy] Roman To Integer (0) | 2022.04.18 |
[LeetCode/easy] PalindromeNumber (0) | 2022.04.18 |