Leetcode-20: Valid Parentheses
来源:互联网 发布:移动网络垃圾 编辑:程序博客网 时间:2024/06/15 06:18
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.
思路: 括号匹配很简单,数据结构中教过大家使用栈来对括号进行匹配,左括号压栈,有对应的右括号出栈,否则返回false,最终的栈中为空即返回true,否则返回false。
public class Solution { public boolean isValid(String s) { Deque<Character> deque = new LinkedList<>(); for (int i = 0; i < s.length(); ++i) { char c = s.charAt(i); if ((c == '(') || (c == '[') || (c == '{')) deque.addFirst(c); else { if (deque.isEmpty()) return false; if (c == ')' && deque.getFirst() != '(' || c == ']' && deque.getFirst() != '[' || c == '}' && deque.getFirst() != '{') return false; deque.removeFirst(); } } return deque.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
- [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
- GUI——鼠标和键盘事件
- 浅谈算法和数据结构: 九 平衡查找树之红黑树
- Java String判等问题
- 安卓平台使用pocketSphinx离线语音识别
- stm32学习笔记(二)按键实验
- Leetcode-20: Valid Parentheses
- 如何使用Android Studio把自己的Android library分享到jCenter和Maven Central
- 如何使输入的数字成为数组的长度?
- Android中View的MeasureSpec以及Measure的过程
- 有趣的排序 (贪心+思维)
- spring mvc java 把多文件打包成zip,并下载
- 补码
- 运算符函数 operator
- 关于一些mybatis异常的原因:org.apache.ibatis.binding.BindingException: Parameter 'status' not found.