有效的括弧匹配
注意事项:
1 使用stack
遇到‘(‘ ‘[‘ ‘{‘就push,遇到‘)‘ ‘]‘ ‘}‘匹配进行pop等操作
class Solution {
public:
bool isValid(string s) {
stack<char> mystack;
char tmp;
for(char c:s){
switch(c){
case ‘(‘:
case ‘[‘:
case ‘{‘:
mystack.push(c);
break;
case ‘)‘:
if(mystack.empty()||mystack.top()!=‘(‘)
return false;
else{
mystack.pop();
break;
}
case ‘]‘:
if(mystack.empty()||mystack.top()!=‘[‘)
return false;
else{
mystack.pop();
break;
}
case ‘}‘:
if(mystack.empty()||mystack.top()!=‘{‘)
return false;
else{
mystack.pop();
break;
}
default:
break;
}
}//end for
return mystack.empty();
}
};