20. Valid Parentheses
https://leetcode.com/problems/valid-parentheses/description/
Thoughts
Code
/*
* @lc app=leetcode id=20 lang=cpp
*
* [20] Valid Parentheses
*/
// @lc code=start
class Solution {
public:
bool isValid(string s) {
stack<char> sta;
for (const auto c : s) {
switch(c) {
case '{':
sta.push('}');
break;
case '[':
sta.push(']');
break;
case '(':
sta.push(')');
break;
default:
if (sta.empty() || sta.top() != c) return false;
sta.pop();
}
}
return sta.empty();
}
};
// @lc code=end
Analysis
Last updated