C++代码:
int i;
char a;
stack<char>str;
if(s.size()==0)
{
return true;
}
else if(s.size()%2==1)
{
return false;
}
else
{
str.push('0');
str.push(s[0]);
for(i=1;i<s.size();i++)
{
if(str.top()=='('&&s[i]==')'||str.top()=='['&&s[i]==']'||str.top()=='{'&&s[i]=='}')
{
str.pop();
}
else
{
str.push(s[i]);
}
}
a=str.top();
if(a=='0')
{
return true;
}
else
{
return false;
}
}
暴力法,遍历,栈;