LeetCode 20: Valid Parentheses
来源:互联网 发布:软件测试工作累吗 编辑:程序博客网 时间:2024/04/29 18:18
Difficulty: 2
Frequency: 5
Problem:
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.
Solution:
class Solution {public: bool isValid(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function if (s.size()==0) return true; if (s.size()&1!=0) return false; vector<char> ParenthesStack; for(int i = 0; i<s.size(); i++) { switch(s[i]){ case '(': case '{': case '[': ParenthesStack.push_back(s[i]); break; case ')': if (ParenthesStack.size()==0||ParenthesStack.back()!='(') return false; else ParenthesStack.pop_back(); break; case '}': if (ParenthesStack.size()==0||ParenthesStack.back()!='{') return false; else ParenthesStack.pop_back(); break; case ']': if (ParenthesStack.size()==0||ParenthesStack.back()!='[') return false; else ParenthesStack.pop_back(); break; default: return false; } } if (ParenthesStack.size()!=0) return false; return true; }};
Note:
Use a stack.
- 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
- 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
- leetcode-20 Valid Parentheses
- leetcode 20 -- Valid Parentheses
- 数组右移
- STL各个容器成员比较表
- 9、从零开始学习JAVA--面向对象的应用
- 排序算法总结
- 初识Lucene
- LeetCode 20: Valid Parentheses
- HDU-1009-FatMouse' Trade
- 排序算法总结与java实现
- java servlet过滤器控制frame框架中的页面跳转
- SSH Filter 过滤器配置
- Hbase深入学习(一) 什么是hbase
- LeetCode - Permutations II
- VSS记住用户名和密码
- Shell中的美元符号$