20. Valid Parentheses--合法的括号组合(使用stack)
2016-11-23 14:39
405 查看
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
这种题目适合用栈来解决。
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
这种题目适合用栈来解决。
class Solution { public: bool isValid(string s) { stack<char> mystack; for(int i = 0; i < s.size(); ++i){ if(s[i]=='('||s[i]=='['||s[i]=='{') mystack.push(s[i]); else{ if(mystack.size()==0) return false; char tmp = mystack.top(); if((tmp=='('&&s[i]==')')||(tmp=='['&&s[i]==']')||(tmp=='{'&&s[i]=='}')) mystack.pop(); else return false; } } if(mystack.size()>0) return false; return true; } };
相关文章推荐
- 20.Valid Parentheses&合法括号序列判断
- [LeetCode] 20. Valid Parentheses 合法括号
- LeetCode20——Valid Parentheses(括号匹配问题,使用栈的知识)
- LeetCode 20. Valid Parentheses(合法括号序列)
- LeetCode 32 Longest Valid Parentheses(最长合法的括号组合)
- leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法
- Valid Parentheses 合法的括号匹配
- 20. Valid Parentheses (括号匹配)
- LeetCode-20 Valid Parentheses(判断括号是否规范)
- 20. Valid Parentheses (Stack)
- 20. Valid Parentheses(stack)
- LeetCode 32 Longest Valid Parentheses 最大合法括号匹配长度计算 动态规划算法有待学习
- 20 Valid Parentheses(匹配括号)
- LeetCode 20 Valid Parentheses(括号匹配)
- [LeetCode] 最长合法括号 longest valid parentheses
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
- java 使用Stack来判断Valid Parentheses
- Valid Parentheses 判断括号组合是否有效@LeetCode
- LeetCode 20 Valid Parentheses(有效的括号)
- LeetCode 20 Valid Parentheses (括号匹配问题)