class Solution { public: /** * * @param s string字符串 * @return bool布尔型 */ bool isValid(string s) { // write code here stack stk;
for(int i = 0; i < s.size(); i ++ )
{
if(s[i] == '(' || s[i] == '[' || s[i] == '{')
{
stk.push(s[i]);
}
if(s[i] == ')')
{
if(stk.empty()) return false;
if(stk.top() == '(')
{
stk.pop();
}
else
{
return false;
}
}
if(s[i] == ']')
{
if(stk.empty()) return false;
if(stk.top() == '[')
{
stk.pop();
}
else
{
return false;
}
}
if(s[i] == '}')
{
if(stk.empty()) return false;
if(stk.top() == '{')
{
stk.pop();
}
else
{
return false;
}
}
}
if(stk.empty()) return true;
return false;
}
};

京公网安备 11010502036488号