【leetcode】20. Valid Parentheses
2017-07-21 18:59
429 查看
@requires_authorization @author johnsondu @create_time 2015.7.13 11:03 @url [valid parentheses](https://leetcode.com/problems/valid-parentheses/) /***************** * 类别: 栈模拟推断 * 时间复杂度: O(n) * 空间复杂度: O(n) ****************/ class Solution { public: bool isValid(string s) { stack<char> st; int len = s.size(); for(int i = 0; i < len; i ++){ if(s[i] == ']' || s[i] == '}' || s[i] == ')'){ if(st.empty()){ return false; } } else{ st.push(s[i]); continue; } if(s[i] == ']'){ char ch = st.top(); st.pop(); if(ch != '[') return false; continue; } if(s[i] == ')'){ char ch = st.top(); st.pop(); if(ch != '(') return false; continue; } if(s[i] == '}'){ char ch = st.top(); st.pop(); if(ch != '{') return false; continue; } } if(st.empty()) return true; else return false; } };
相关文章推荐
- [LeetCode]20 Valid Parentheses
- LeetCode 20 Valid Parentheses (括号匹配问题)
- [leetcode 20] Valid Parentheses
- leetcode 20. Valid Parentheses
- [Leetcode] 20. Valid Parentheses
- [LeetCode] 20. Valid Parentheses
- 【leetcode】20—valid parentheses
- Leetcode 20. Valid Parentheses - javascript
- leetcode 20. Valid Parentheses
- Leetcode 20. Valid Parentheses
- [LeetCode] 20. Valid Parentheses 合法括号
- 20. Valid Parentheses LeetCode
- LeetCode 20:Valid Parentheses
- LeetCode 20 Valid Parentheses
- LeetCode 20. Valid Parentheses
- LeetCode 20. Valid Parentheses
- 【leetcode】20. Valid Parentheses
- LeetCode *** 20. Valid Parentheses
- LeetCode 20. Valid Parentheses
- LeetCode 20 Valid Parentheses