class Solution {
public:
bool isValid(string s) {
stack<char> st;
for(int i=0;i<s.length();i++)
{
if(s[i]=='('||s[i]=='['||s[i]=='{')
st.push(s[i]);
else
{
if(st.empty())
return false;
switch(s[i])
{
case ')':
if(st.top()!='(')
return false;
else
st.pop();
break;
case ']':
if(st.top()!='[')
return false;
else
st.pop();
break;
case '}':
if(st.top()!='{')
return false;
else
st.pop();
break;
}
}
}
if(!st.empty())
return false;
return true;
}
};
leetcode 20: Valid Parentheses
最新推荐文章于 2021-05-19 14:51:40 发布