LeetCode----20. Valid Parentheses
2016-09-20 14:59
507 查看
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.
Subscribe to see which companies asked this question
"()[]" "(" "(()[])" "([()])"
Runtime Error Message:Line
8: java.lang.StringIndexOutOfBoundsException: String index out of range: 2
Last executed input:"(("
"[({(())}[()])]"
'(',
')',
'{',
'}',
'['and
']',
determine if the input string is valid.
The brackets must close in the correct order,
"()"and
"()[]{}"are
all valid but
"(]"and
"([)]"are
not.
Subscribe to see which companies asked this question
"()[]" "(" "(()[])" "([()])"
Runtime Error Message:Line
8: java.lang.StringIndexOutOfBoundsException: String index out of range: 2
Last executed input:"(("
"[({(())}[()])]"
public class Solution { public boolean isValid(String s) { Stack<Character> stack = new Stack<Character>(); for (Character c : s.toCharArray()) { if ("({[".contains(String.valueOf(c))) { stack.push(c); } else { if (!stack.isEmpty() && is_valid(stack.peek(), c)) { stack.pop(); } else { return false; } } } return stack.isEmpty(); } private boolean is_valid(char c1, char c2) { return (c1 == '(' && c2 == ')') || (c1 == '{' && c2 == '}') || (c1 == '[' && c2 == ']'); } }
相关文章推荐
- [LeetCode]20. Valid Parentheses
- 【leetcode】20. Valid Parentheses
- LeetCode 20. Valid Parentheses(括号校验)
- [leetcode]20:Valid Parentheses
- Leetcode - 20. Valid Parentheses
- LeetCode 20. Valid Parentheses
- [LeetCode20]Valid Parentheses
- LeetCode: 20. Valid Parentheses
- LeetCode 20: Valid Parentheses
- LeetCode之20---Valid Parentheses
- [勇者闯LeetCode] 20. Valid Parentheses
- leetcode 20. Valid Parentheses ---java
- LeetCode--20. Valid Parentheses
- leetcode 20 Valid Parentheses
- LeetCode 20 : Valid Parentheses (Java)
- 【Leetcode】20. Valid Parentheses
- Leetcode 20 Valid Parentheses stack的应用
- leetcode 20 Valid Parentheses
- leetcode20:Valid Parentheses
- python写算法题:leetcode: 20. Valid Parentheses