思路:
代码:
class Solution {
public:
bool isValid(string s) {
stack<char> stk;
unordered_map<char,char> hash;
hash['('] = ')';
hash['['] = ']';
hash['{'] = '}';
for(auto &c : s){
if(c == '(' || c == '[' || c == '{') stk.push(c);
else{
if(stk.empty() || hash[stk.top()] != c ) return false;
else{
stk.pop();
}
}
}
if(stk.size()) return false;
return true;
}
};
评论区