熟悉栈的水题
class Solution
{
public:
bool isValid(string s)
{
int len = s.size();
stack<char> sta;
for (int i = 0; i < len; i++)
{
if (s[i] == '(' || s[i] == '[' || s[i] == '{')
sta.push(s[i]);
else if (sta.empty())
return false;
else
{
if (s[i] == ')')
{
if (sta.top() != '(')
return false;
else
sta.pop();
}
else if (s[i] == ']')
{
if (sta.top() != '[')
return false;
else
sta.pop();
}
else if (s[i] == '}')
{
if (sta.top() != '{')
return false;
else
sta.pop();
}
}
}
return sta.empty();
}
};