class Solution {
public:
bool isValid(string s) {
stack<char> st;
st.push('0');
if(s[0] == ')' || s[0] == ']' || s[0] == '}') return false;
for(int i = 0;i<s.size();i++)
{
if(s[i] == '(' || s[i] == '[' || s[i] == '{')
{
st.push(s[i]);
}
else if(s[i] == ')')
{
if(st.top() != '(') return false;
st.pop();
}
else if(s[i] == '}')
{
if(st.top() != '{') return false;
st.pop();
}
else
{
if(st.top() != '[') return false;
st.pop();
}
}
if(st.top() == '0') return true;
return false;
}
};
leetcode每日一题第三十八天
最新推荐文章于 2024-09-20 15:28:15 发布