LeetCode 20. Valid Parentheses
来源:互联网 发布:迅捷聊天记录恢复软件 编辑:程序博客网 时间:2024/05/18 02:14
原题:(频率5)
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.
题意:就是判断括号是否对称
代码和思路:
很简单,用一个栈,碰到open括号,就把close括号加入栈。遇到close括号,就弹出栈顶元素,看是否相等。
class Solution { public boolean isValid(String s) { Stack<Character> stack = new Stack<>(); 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(); }}
阅读全文
0 0
- 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
- 20. Valid Parentheses LeetCode
- 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
- 2017第十届中国品牌加盟加盟投资博览会会刊(参展商名录)
- ROW_NUMBER() OVER函数的基本用法
- Android MVVM实战Demo完全解析
- BZOJ 1857 [Scoi2010]传送带 三分套三分
- WIN7 企业版激活
- LeetCode 20. Valid Parentheses
- AlertDialog 底部弹出去掉边距
- 线程同步(以下介绍三种方)
- List对象转化为JSON数据的操作类和应用
- cocos2dx 3.x(常见的46种动作)
- 适配iOS 11及iPhoneX iOS 11下tableView内容下移的问题
- 数据结构的非经典解法
- fold,foldLeft和foldRight区别与联系
- 安卓播放播放音乐