class Solution {
public:
bool isValid(string s) {
int len = s.length();
if( len & 1 || !len)
return false;
stack<char> sta;
for(int i = 0; i < len; i ++)
{
if( s[i] == '(' || s[i] == '{' || s[i] == '[')
sta.push(s[i]);
else if( sta.size() && ( (s[i] - sta.top()) == 2 || (s[i] - sta.top()) == 1 ) )
sta.pop();
else return false;
}
if( sta.size())
return false;
else return true;
}
};
Valid Parentheses
最新推荐文章于 2021-02-23 15:41:56 发布