您的位置:首页 > 其它

leetcode20. Valid Parentheses

2017-11-05 00:31 417 查看
leetcode20. Valid Parentheses

思路:

做一个栈,遇到对称的符号则出栈,否则入栈,最后判断栈是否为空。

class Solution {
public:
bool isValid(string s) {
string stack(s);
int top = -1;
if(s.size()<=0)
return true;
for(int i=0;i<s.size();i++){
switch(s[i]){
case '(':
case '{':
case '[':
stack[++top] = s[i];
break;
case ')':
if(top<0 || stack[top]!='(') return false;
top--;
break;
case '}':
if(top<0 || stack[top]!='{') return false;
top--;
break;
case ']':
if(top<0 || stack[top]!='[') return false;

top--;
break;
default:
return false;
}

}

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