LeetCode Valid Parentheses
2014-10-25 13:04
176 查看
Given a string containing just the characters
The brackets must close in the correct order,
'(',
')',
'{',
'}',
'['and
']', determine if the input string is valid.
The brackets must close in the correct order,
"()"and
"()[]{}"are all valid but
"(]"and
"([)]"are not.
public class Solution { public boolean isValid(String s) { if (s.equals("")) { return true; } Stack<Character> stack=new Stack<>(); stack.push('s'); for (char c : s.toCharArray()) { if (c=='(' || c=='[' || c=='{') { stack.push(c); }else { if (c==')' && stack.pop()!='(') { return false; } if (c==']' && stack.pop()!='[') { return false; } if (c=='}' && stack.pop()!='{') { return false; } } } if (stack.pop()!='s') { return false; } return true; } }
相关文章推荐
- LeetCode 20. Valid Parentheses
- LeetCode-20. Valid Parentheses
- LeetCode小白菜笔记[6]:Valid Parentheses
- leetcode第20题——*Valid Parentheses
- LeetCode之“动态规划”:Valid Parentheses && Longest Valid Parentheses
- 【Leetcode】【python】Longest Valid Parentheses
- [LeetCode] Valid Parentheses
- leetcode_num20_Valid Parentheses
- LeetCode-20-Valid Parentheses(堆栈)-Easy
- Longest Valid Parentheses_Leetcode_#32
- LeetCode-20-Valid Parentheses(有效的括号)
- LeetCode Valid Parentheses
- [LeetCode] Valid Parentheses
- LeetCode-Longest Valid Parentheses
- LeetCode---(32)Longest Valid Parentheses
- [leetcode 20] Valid Parentheses
- LeetCode Valid Parentheses
- [Leetcode]Valid Parentheses
- LeetCode力扣之Valid Parentheses
- LeetCode(20)Valid Parentheses