class Solution {
public:
bool isValid(string s) {
stack<char> store;
for(int i = 0; i < s.size(); i++)
{
if(s[i] == '(')
{
store.push(')');
}
else if(s[i] == '[')
{
store.push(']');
}
else if(s[i] == '{')
{
store.push('}');
}
else if(store.empty() || store.top() != s[i])
{
return false;
}
else
{
store.pop();
}
}
return store.empty() ? true:false;
}
};
【LeetCode热题100道】20. 有效的括号
最新推荐文章于 2025-05-20 18:02:11 发布