[LeetCode] 20 - Valid Parentheses
문제
- 링크: https://leetcode.com/problems/valid-parentheses
- 난이도: Easy
- 태그: 문자열, 스택
- 결과:
Time: 0 ms (100.00%), Space: 8.8 MB (88.02%)
풀이
class Solution {
public:
bool isValid(string s) {
stack<char> st;
for (char ch : s)
{
if (isOpening(ch))
{
st.push(ch);
continue;
}
if (st.empty())
{
return false;
}
char t = st.top();
st.pop();
if ((ch == ')' && t == '(')
|| (ch == ']' && t == '[')
|| (ch == '}' && t == '{'))
{
continue;
}
return false;
}
return st.empty();
}
private:
bool isOpening(char c)
{
switch(c)
{
case '(':
case '[':
case '{':
return true;
}
return false;
}
};
음, 이건 뭐 스택을 처음 배울 때 푸는 문제 같은 거니까 설명은 생략!
댓글남기기