class Solution {
public:
bool isValid(string s) {
if (s.empty()) {return true;}
stack<char> t;
for (auto itr = s.begin(); itr != s.end(); ++itr) {
if (*itr == '(' || *itr == '[' || *itr == '{') {
t.push(*itr);
}
else {
if (t.empty()) {return false;}
char c = t.top();
if (*itr == ')' && c == '(') {t.pop();}
else if (*itr == ']' && c == '[') {t.pop();}
else if (*itr == '}' && c == '{') {t.pop();}
else {return false;}
}
}
if (t.empty()) {return true;}
else {return false;}
}
};
Valid Parentheses
最新推荐文章于 2021-02-23 15:41:56 发布