有效括号序列_牛客题霸_牛客网
bool isValid(string s) {
// write code here
stack<char> stk;
for(int i=0; i<s.size(); i++)
{
if(s[i] == '(')
{
stk.push(')');
}
else if(s[i] == '{')
{
stk.push('}');
}
else if(s[i] == '[')
{
stk.push(']');
}
else
{
if(stk.empty() || s[i] != stk.top())
return false;
stk.pop();
}
}
return stk.empty();
}
bool isValid(string s) {
// write code here
if(s.length()%2 != 0)
return false;
stack<char> stk;
for(int i=0; i<s.size(); i++)
{
if(s[i] == '(')
{
stk.push(')');
}
else if(s[i] == '{')
{
stk.push('}');
}
else if(s[i] == '[')
{
stk.push(']');
}
else if(stk.empty() || s[i] != stk.top())
{
return false;
}
else {
stk.pop();
}
}
return stk.empty();
}