题目
代码
class Solution {
public:
bool isValid(string s) {
stack<char> sta;
for(auto i:s)
{
switch(i)
{
case '[':
case '(':
case '{':sta.push(i);break;
case ']':if(sta.empty()||sta.top()!='[')return false;
else sta.pop();break;
case ')':if(sta.empty()||sta.top()!='(')return false;
else sta.pop();break;
case '}':if(sta.empty()||sta.top()!='{')return false;
else sta.pop();break;
}
}
return sta.empty();
}
};
思路
利用一个辅助栈来存储 [、(、{