您的位置:首页 > 其它

20. Valid Parentheses

2016-09-11 20:11 183 查看
使用栈

class Solution {
public:
bool isValid(string s) {
stack<char> st;
int n=s.size();
int i;
for(i=0;i<n;i++)
{
if(s[i]==')')
{
if(!st.empty()&&st.top()=='(')
st.pop();
else
return false;
}
else if(s[i]==']')
{
if(!st.empty()&&st.top()=='[')
st.pop();
else
return false;
}
else if(s[i]=='}')
{
if(!st.empty()&&st.top()=='{')
st.pop();
else
return false;
}
else
st.push(s[i]);
}
if(st.empty())
return true;
return false;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: