leetcode-20. Valid Parentheses
2017-03-08 17:00
471 查看
leetcode-20. Valid Parentheses
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. Subscribe to see which companies asked this question.
栈的使用,明白pop是不要参数的,直接p.pop( )
class Solution { public: bool isValid(string s) { stack<char> p; for(int i = 0; i < s.size(); ++i){ if(s[i] == '(' || s[i] == '[' || s[i] == '{') p.push(s[i]); else{ if(p.empty()) return false; if(s[i] == ')' && p.top() != '(') return false; if(s[i] == ']' && p.top() != '[') return false; if(s[i] == '}' && p.top() != '{') return false; else p.pop(); //栈的操作,pop的时候直接pop就行,不用加参数 } } if(p.empty()) return true; else return false; } };
相关文章推荐
- leetcode 20. Valid Parentheses
- LeetCode-20-Valid Parentheses(有效的括号)
- leetcode解题报告20. Valid Parentheses
- leetcode_20_Valid Parentheses (easy)
- LeetCode 20 -- Valid Parentheses
- [Leetcode] 20. Valid Parentheses
- leetcode 20. Valid Parentheses
- 20. Valid Parentheses-leetcode-java
- [leetcode]20. Valid Parentheses(java)
- Leetcode 20. Valid Parentheses
- leetcode题解-20. Valid Parentheses
- Leetcode 20 Valid Parentheses 判断括号匹配
- [leetcode Q20] Valid Parentheses
- LeetCodet题解--20. Valid Parentheses(括号匹配问题)
- LeetCode20:Valid Parentheses
- [leetcode 20] Valid Parentheses
- leetcode 20. Valid Parentheses
- Leetcode_20_Valid Parentheses
- Leetcode 20. Valid Parentheses (Easy) (cpp)
- LeetCode_20---Valid Parentheses