- third attempt:
vector<char> single;
if(s.size()%2!=0)
return 0;
for(unsigned int i = 0 ; i < s.size();)
switch(s[i])
{
case '(':
if(s[i+1]!=')')
{
single.push_back('(');
i=i+1;
}
else
i=i+2;
break;
case '{':
if(s[i+1]!='}')
{
single.push_back('{');
i=i+1;
}
else
i=i+2;
break;
case '[':
if(s[i+1]!=']')
{
single.push_back('[');
i=i+1;
}
else
i = i+2;
break;
case ')':
if(!single.empty() && single.back()=='(')
{
single.pop_back();
i=i+1;
break;
}
else
return 0;
case']':
if(!single.empty()&&single.back()=='[')
{
single.pop_back();
i=i+1;
break;
}
else
return 0;
case '}':
if(!single.empty()&&single.back()=='{')
{
single.pop_back();
i=i+1;
break;
}
else
return 0;
}
if(single.empty())
return 1;
else
return 0;
易错点:
检查vector是否为空
i的增长是+1还是+2