[Leetcode] Valid Parentheses (Java)
2013-12-26 15:08
387 查看
Given a string containing just the characters
determine if the input string is valid.
The brackets must close in the correct order,
all valid but
not.
括号匹配,没考虑//,/* */," "等
'(',
')',
'{',
'}',
'['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 ValidParentheses { public boolean isValid(String s) { Stack<Character> stack = new Stack<Character>(); for(int i=0;i<s.length();i++){ char c = s.charAt(i); if(c=='('||c=='[') stack.add(c); else if(c==')'){ if(!stack.isEmpty()&&stack.peek()=='(') stack.pop(); else return false; }else if(c==']'){ if(!stack.isEmpty()&&stack.peek()=='[') stack.pop(); else return false; } } if(stack.isEmpty()) return true; return false; } public static void main(String[] args) { String s = "(([()]))[]"; System.out.println(new ValidParentheses().isValid(s)); } }
相关文章推荐
- 【LeetCode-面试算法经典-Java实现】【020-Valid Parentheses(括号验证)】
- [leetcode]20. Valid Parentheses(java)
- [Leetcode] Longest Valid Parentheses (Java)
- Longest Valid Parentheses leetcode java
- 【leetcode】Valid Parentheses【java】
- leetcode 第20题<Valid Parentheses>(java)
- [leetcode]20. Valid Parentheses(Java)
- 【Leetcode】Valid Parentheses in JAVA——一个括号de了20分钟bug的故事
- 【LeetCode-面试算法经典-Java实现】【032-Longest Valid Parentheses(最长有效括号)】
- Java for LeetCode 032 Longest Valid Parentheses
- [LeetCode][Java] Valid Parentheses
- leetcode Valid Parentheses(Java)
- leetcode 20. Valid Parentheses ---java
- LeetCode : Longest Valid Parentheses [java]
- 【JAVA、C++】LeetCode 020 Valid Parentheses
- LeetCode 20 : Valid Parentheses (Java)
- 20. Valid Parentheses-leetcode-java
- [leetcode]32. Longest Valid Parentheses(Java)
- leetcode:Valid Parentheses 【Java】
- LeetCode : Valid Parentheses [java]