leetcode20 C++ 4ms 有效的括号 微软面试题
class Solution {
public:
bool isValid(string s) {
vector<char> stack;
map<char, char> bra;
bra[')'] = '(';
bra['}'] = '{';
bra[']'] = '[';
for(int i=0;i<s.size();i++){
if(s[i]=='{' || s[i]=='[' || s[i]=='('){
stack.push_back(s[i]);
}
else{
if(stack.empty() || bra[s[i]] != stack[stack.size()-1]){
return false;
}
stack.pop_back();
}
}
return stack.empty();
}
};