20. Valid Parentheses
来源:互联网 发布:知乎日报web电脑版 编辑:程序博客网 时间:2024/04/29 09:29
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.
思路:堆栈的一个应用。
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.empty() && checkValid(stack.top(),s[i])) { stack.pop(); } else { return false; } } } return stack.empty(); } bool checkValid(char c1, char c2) { return (c1 == '(' && c2 == ')') || (c1 == '{' && c2 == '}') || (c1 == '[' && c2 == ']'); }};
0 0
- LeetCode 20. Valid Parentheses
- [LeetCode] 20.Valid Parentheses
- LeetCode --- 20. Valid Parentheses
- LeetCode 20.Valid Parentheses
- [Leetcode] 20. Valid Parentheses
- 20.Valid Parentheses
- 20.Valid Parentheses
- 20. Valid Parentheses
- 【leetcode】20. Valid Parentheses
- [leetcode] 20.Valid Parentheses
- [leetcode] 20. Valid Parentheses
- 20. Valid Parentheses
- 20. Valid Parentheses
- 20. Valid Parentheses
- Leetcode 20. Valid Parentheses
- 20. Valid Parentheses LeetCode
- 20. Valid Parentheses
- 20. Valid Parentheses
- 递归删除.svn文件夹以及文件
- 数组排序-插入排序
- 团队协同开发----项目托管攻略
- Sliding Menu滑动菜单简单使用
- PHP正则表达式总结
- 20. Valid Parentheses
- 数据库领域的三大会议
- html代码初体验
- Cable master
- 1112. Stucked Keyboard (20)
- UVA 748-Exponentiation
- HDU 5753 Permutation Bo (找规律)
- Android Volley完全解析(郭林老师作品)
- 顺序表应用6:有序顺序表查询