您的位置:首页 > 其它

LeetCode 20. Valid Parentheses

2017-12-19 11:03 429 查看
class Solution {
public:
bool isValid(string s) {
stack<char> stack;
for(int i=0;i<s.size();i++){
if(s[i]=='{'||s[i]=='['||s[i]=='(')
stack.push(s[i]);
else{
if(stack.size()==0)
return false;

char c=stack.top();
stack.pop();

char match;
if(s[i]=='}')
match='{';
else if(s[i]==')')
match='(';
else{
assert(s[i]==']');
match='[';
}

if(c!=match)
return false;
}
}
if(stack.size()!=0)
return false;
return true;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode