括号匹配问题,使用栈的特点,匹配则出栈,否则入栈,最后栈为空则全部匹配。代码如下:
class Solution {
public:
bool isValid(string s) {
stack<char> T;
for(int i = ;i < s.length();i ++)
{
if((T.empty()) || (s[i] == T.top()) || abs(s[i] - T.top()) > )
{
T.push(s[i]);
}
else T.pop();
}
if(T.empty())
return true;
else
return false;
}
};