class Solution {
public:
bool isValid(string s) {
stack<char> a;
char b;
for(char c:s)
{
if(c=='}')
{
if(a.empty())
{
return false;
}
b=a.top();
if(b=='{')
{
a.pop();
}
else
{
return false;
}
}
else if(c==']')
{
if(a.empty())
{
return false;
}
b=a.top();
if(b=='[')
{
a.pop();
}
else
{
return false;
}
}
else if(c==')')
{
if(a.empty())
{
return false;
}
b=a.top();
if(b=='(')
{
a.pop();
}
else
{
return false;
}
}
else
{
a.push(c);
}
}
if(!a.empty())
{
return false;
}
else
{
return true;
}
}
};
leetcode 20.有效的括号
最新推荐文章于 2025-06-05 14:05:55 发布