这样写简单些!
class Solution {
public:
bool isValid(string s) {
stack<char> st;
map<char, char> mp;
mp[')'] = '(', mp['}'] = '{', mp[']'] = '[';
for(char c: s){
if(c == '(' || c == '{' || c == '[') st.push(c);
else{
if(st.empty() || st.top() != mp[c]) return false;
st.pop();
}
}
return st.empty();
}
};