class Solution {
public:
bool isValid(string s) {
stack<char>stk;
map<char,char>hash;
hash[')'] = '(';
hash[']'] = '[';
hash['}'] = '{';
for(int i = 0; i < s.size(); i++)
{
if(s[i] == '(' || s[i] == '[' || s[i] == '{')
{
stk.push(s[i]);
}
else if(!stk.empty())
{
char ch = stk.top();
if(ch == hash[s[i]])
{
stk.pop();
}
else
{
return false;
}
}
else
{
return false;
}
}
return stk.empty();
}
};
[leetcode]20. 有效的括号
最新推荐文章于 2024-09-03 18:29:17 发布