代码随想录:有效的括号
class Solution {
public:
bool isValid(string s) {
stack<char> a;
for (char target : s) {
if (target == '(' || target == '{' || target == '[') {
a.push(target);
} else if (!a.empty() && target == ')') {
if (a.top() == '(')
a.pop();
else
return false;
} else if (!a.empty() && target == '}') {
if (a.top() == '{')
a.pop();
else
return false;
} else if (!a.empty() && target == ']') {
if (a.top() == '[')
a.pop();
else
return false;
}else{
return false;
}
}
if (a.empty()) {
return true;
} else {
return false;
}
}
};