题目链接
栈的经典教学级题目了
class Solution {
public:
bool isValid(string s) {
if(s.length()%2 == 1)
return false; //剪枝
stack<char> tmp;
for(int i = 0; i < s.length(); i++){
if(tmp.empty() || s[i]=='(' || s[i]=='[' || s[i]=='{')
tmp.push(s[i]);
else if((tmp.top()=='('&&s[i]==')') || (tmp.top()=='['&&s[i]==']') || (tmp.top()=='{'&&s[i]=='}'))
tmp.pop();
else
tmp.push(s[i]);
}
if(tmp.empty())
return true;
else
return false;
}
};