有效的括号匹配
bool isValid(string s) {
if(s.size() == 0)
{
return false;
}
stack<char>stackHelp;;
for(int i = 0; i < s.length(); i ++)
{
if(s[i] == '(' || s[i] == '{' || s[i] == '[')
{
stackHelp.push(s[i]);
}
if(s[i] == ')' || s[i] == '}' || s[i] == ']')
{
if(stackHelp.empty())
{
return false;
}
if(s[i] == ')' && stackHelp.top() != '(')
{
return false;
}
if(s[i] == '}' && stackHelp.top() != '{')
{
return false;
}
if(s[i] == ']' && stackHelp.top() != '[')
{
return false;
}
stackHelp.pop();
}
}
if(stackHelp.empty())
{
return true;
}
else
{
return false;
}
}
网友评论