class Solution {
public:
/**
*
* @param s string字符串
* @return bool布尔型
*/
bool isValid(string s) {
// write code here
stack<char>stacks;
for(int i=0;i<s.length();i++)
{
if(s[i]=='('||s[i]=='['||stacks.empty()||s[i]=='{')//为空或者是左括号就进栈
stacks.push(s[i]);
if(s[i]==')')//是右括号时判断当前栈顶是否匹配匹配就出栈
{
if(stacks.top()=='(')
{
stacks.pop();
}
else
return false;//不匹配直接返回false
}
if(s[i]==']')
{
if(stacks.top()=='[')
stacks.pop();
else
return false;
}
if(s[i]=='}')
{
if(stacks.top()=='{')
stacks.pop();
else
return false;
}
}
if(stacks.empty())//最后判断栈是否为空 为空返回true
return true;
else
return false;
}
};