这个就是判断括号的匹配,也很好写,限制也很少。。。
其实就是栈的使用,别的也没有什么,但是我的解法是把整个字符串都遍历一遍再去判空,其实不用,判断过程中只要有不匹配的就可以直接return
class Solution {
public:
bool isValid(string s) {
int len = s.size();
if(len%2!=0)
return false;
stack<char> sta;
for(int i = 0;i<s.size();i++)
{
if(s[i]=='('||s[i]=='['||s[i]=='{')
sta.push(s[i]);
else if(s[i]==')'&&!sta.empty()&&sta.top()=='(')
sta.pop();
else if(s[i]==']'&&!sta.empty()&&sta.top()=='[')
sta.pop();
else if(s[i]=='}'&&!sta.empty()&&sta.top()=='{')
sta.pop();
}
if(sta.empty())
return true;
else return false;
}
};