Valid Parentheses_Leetcode_#20
来源:互联网 发布:上海社保积分怎么算法 编辑:程序博客网 时间:2024/06/16 23:16
1.题目
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.
2 解法
时间复杂度O(N)
public class Solution { public boolean isValid(String s) { ArrayList<Character> arr = new ArrayList<Character>(); for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (ch == '(' || ch == '{' || ch == '[') { arr.add(ch); }else if (ch == ')' && !arr.isEmpty() && arr.get(arr.size() - 1) == '(') { arr.remove(arr.size() - 1); }else if (ch == '}' && !arr.isEmpty() && arr.get(arr.size() - 1) == '{') { arr.remove(arr.size() - 1); }else if (ch == ']' && !arr.isEmpty() && arr.get(arr.size() - 1) == '[') { arr.remove(arr.size() - 1); }else { return false; } } return arr.isEmpty() ? true : false; }}
0 0
- Valid Parentheses_Leetcode_#20
- Longest Valid Parentheses_Leetcode_#32
- Generate Parentheses_Leetcode_#22
- LeetCode 20: Valid Parentheses
- LeetCode(20) Valid Parentheses
- leetcode 20 Valid Parentheses
- Leetcode【20】:Valid Parentheses
- leetcode 20-Valid Parentheses
- [leetcode 20] Valid Parentheses
- 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
- 九个Console命令,让js调试更简单
- HDU 2159 FATE (二维完全背包)
- win7装机
- DataBinding的基本使用(四)
- mybatis 一对多,多对一
- Valid Parentheses_Leetcode_#20
- 如何做项目技术选型
- DataBinding的基本使用(五)
- 一篇文章弄懂CSS3的content属性
- zookeeper 介绍(译)
- 解决方案之任务队列
- Android Studio把Eclipse android项目当作依赖库lib来使用的一些问题解决
- *[Lintcode]Binary Tree Maximum Path Sum
- leetcode题目总结<2>