您的位置:首页 > 其它

leetcode 20: Valid Parentheses

2015-07-03 11:58 267 查看
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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: