[leetcode] 20. Valid Parentheses 解题报告
来源:互联网 发布:淘宝评价过期有信誉 编辑:程序博客网 时间:2024/05/28 11:50
题目链接:https://leetcode.com/problems/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.
思路:利用一个栈来保存需要匹配的左括号,碰到右括号则判断栈顶需要匹配的是不是此类型的括号,最后栈为空即左括号完全被匹配则为合法字符串。
代码如下:
class Solution {public: bool isValid(string s) { stack<char> st; for(auto val: s) { if(val == '(' || val == '{' || val == '[') st.push(val); if(val == ')') { if(st.empty() || st.top()!= '(') return false; else st.pop(); } if(val == '}') { if(st.empty() || st.top()!= '{') return false; else st.pop(); } if(val == ']') { if(st.empty() || st.top()!= '[') return false; else st.pop(); } } return st.empty(); }};
0 0
- [leetcode] 20. Valid Parentheses 解题报告
- [Leetcode] 20. Valid Parentheses 解题报告
- leetcode解题报告20. Valid Parentheses
- LeetCode解题报告--Valid Parentheses
- [LeetCode] Valid Parentheses 解题报告
- [LeetCode] Valid Parentheses 解题报告
- 【LeetCode】Valid Parentheses 解题报告
- Leetcode Valid Parentheses 解题报告
- [LeetCode]Longest Valid Parentheses, 解题报告
- LeetCode Longest Valid Parentheses 解题报告
- [LeetCode] Longest Valid Parentheses 解题报告
- 【LeetCode】Longest Valid Parentheses 解题报告
- [leetcode] 32. Longest Valid Parentheses 解题报告
- [Leetcode] 32. Longest Valid Parentheses 解题报告
- Leetcode 32 Longest Valid Parentheses 解题报告。
- [leetcode javascript解题]Valid Parentheses
- LeetCode解题-#20-Valid Parentheses
- Valid Parentheses——解题报告
- PHP 7 值得期待的新特性(下)
- IOS面试算法题(4)——反斜对角线打印矩阵
- LeetCode 290:Word Pattern
- 最高分是多少(华为机试)
- python核心编程-标准类型派生-可变类型
- [leetcode] 20. Valid Parentheses 解题报告
- 用python写一个windows下的定时关机脚本
- 一种可能的新的人脑信息网络模型
- BZOJ2132: 圈地计划
- Sublime Text配置集锦
- 混淆编译-proguard
- Maven学习系列--04Maven深入
- 分享代码格式的js书写
- Unity Text加入超链接