20. Valid Parentheses
来源:互联网 发布:淘宝2017虚假交易处罚 编辑:程序博客网 时间:2024/06/06 18:53
Description:
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:
1. How to match?
Use Stack to match.
Every time you input a ( , [ or { , you can push a ) , ] or } into the stack to match later.
Also, we need to check the null at last. // return stack.isEmpty();
Code:
public boolean isValid(String s) {Stack<Character> stack = new Stack<Character>();for (char c : s.toCharArray()) {if (c == '(')stack.push(')');else if (c == '{')stack.push('}');else if (c == '[')stack.push(']');else if (stack.isEmpty() || stack.pop() != c)return false;}return stack.isEmpty();}
Lesson learned:
1. Stack
2. Check the null.
阅读全文
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
- apache hive + sentry测试
- java代码获取服务器的地址
- php根据GD库生成透明背景图和和合成图片
- 末尾0的个数
- SolrDemo
- 20. Valid Parentheses
- 欢迎使用CSDN-markdown编辑器
- 华为笔试题--LISP括号匹配 解析及源码实现
- 矩阵局部最大值
- iOS 开发之沙盒机制
- STL中的hash_map使用
- ETH挖矿显卡选型和矿机配置
- requests和BeautifulSoup一些细节
- C#数据类型