20. Valid Parentheses
来源:互联网 发布:网络盒子加密软件 编辑:程序博客网 时间:2024/06/05 21:03
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.
题目要求判断括号是否匹配,可以用栈来做。
stl stack的用法:http://www.cplusplus.com/reference/stack/stack/
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()) return false; if ((s[i] == ')' && stack.top() == '(') || (s[i] == ']' && stack.top() == '[') || (s[i] == '}' && stack.top() == '{')) stack.pop(); else return false; } } return stack.empty(); }};
阅读全文
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
- 9.16 第四题 数论 欧拉筛+唯一分解定理
- 小米笔试题第1题
- am335x 内核源码分析2 LCD移植
- hihoCoder1331—扩展二进制数
- 异步加载数据,无限轮播,
- 20. Valid Parentheses
- caffe:把pb格式的文件均值(mean.binaryproto)转换成numpy格式的文件均值(mean.npy)
- poj 3177 Redundant Paths(Tarjan,边双连通分量)
- 机器学习-1-.py文件的导入问题
- iOS GPUImage研究序一:内置滤镜
- 深入理解MySQL索引
- yii2 表单小部件默认选中的方法
- PullToRefreshListView刷新下拉加载最简单用法
- Xgboost4j打包 java Ubuntu安装