LeetCode : Valid Parentheses [java]
来源:互联网 发布:电脑编程有几种 编辑:程序博客网 时间:2024/04/27 21:54
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.
思路:使用堆栈存储,遇到配对就弹出,为空则恰好全部配对成功。
import java.util.Stack;public class Solution { public boolean isValid(String s) {Stack<Character> stack = new Stack<>();for (int i = 0; i < s.length(); i++) {char ch = s.charAt(i);switch (ch) {case ')':if (!stack.isEmpty() && stack.peek() == '(') {stack.pop();} else {stack.push(ch);}break;case ']':if (!stack.isEmpty() && stack.peek() == '[') {stack.pop();} else {stack.push(ch);}break;case '}':if (!stack.isEmpty() && stack.peek() == '{') {stack.pop();} else {stack.push(ch);}break;default:stack.push(ch);}}return stack.isEmpty(); }}
1 0
- [Leetcode] Valid Parentheses (Java)
- Valid Parentheses leetcode java
- [LeetCode][Java] Valid Parentheses
- 【leetcode】Valid Parentheses【java】
- Valid Parentheses LeetCode java
- leetcode:Valid Parentheses 【Java】
- LeetCode : Valid Parentheses [java]
- LeetCode-JAVA-Valid Parentheses
- leetcode Valid Parentheses(Java)
- Leetcode Valid Parentheses(java)
- [Leetcode] Longest Valid Parentheses (Java)
- LeetCode – Valid Parentheses (Java)
- [LeetCode][Java] Longest Valid Parentheses
- [leetcode-20]Valid Parentheses(java)
- LeetCode 20 : Valid Parentheses (Java)
- leetcode:Longest Valid Parentheses 【Java】
- LeetCode : Longest Valid Parentheses [java]
- 20. Valid Parentheses-leetcode-java
- DOM增删改html元素
- arm看门狗的作用
- HDU-3613 Best Reward (Manacher)
- 关于Math这个类的一些基本方法的案例实现
- [Offer收割]编程练习赛1 hihocoder 1269 优化延迟 (二分+优先权队列)
- LeetCode : Valid Parentheses [java]
- Bitmap优化问题
- 331. Verify Preorder Serialization of a Binary Tree
- 动态转移方程 免费馅饼
- qsort函数用法
- python小点心--获取汉子拼音首字母
- 《Java实战开发经典》第六章6.1
- nyoj 首尾相连数组的最大子数组和
- SSL编程