class Solution {
public:
bool isValid(string s) {
// write code here
stack<char> comp_stack;
for (char& c : s) {
if (c == '(' || c == '{' || c == '[') {
comp_stack.push(c);
continue;
}
if(comp_stack.empty())
return false;
char top = comp_stack.top();
if (top == '(' && c == ')') {
comp_stack.pop();
continue;
}
if (top == '[' && c == ']') {
comp_stack.pop();
continue;
}
if (top == '{' && c == '}') {
comp_stack.pop();
continue;
}
return false;
}
return comp_stack.size() == 0 ? true : false;
}
};

京公网安备 11010502036488号