forked from yunyinl/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidParentheses.java
More file actions
33 lines (28 loc) · 1.13 KB
/
Copy pathValidParentheses.java
File metadata and controls
33 lines (28 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package amazon;
// 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;
/**
* 括号匹配问题,用一个栈来解决
* leetcode20 -> https://leetcode.com/problems/valid-parentheses
* Created by anduo on 17-3-13.
*/
public class ValidParentheses {
public boolean isValid(String input) {
if (input == null || input.length() < 2) return false;
char[] chars = input.toCharArray();
Stack<Character> stack = new Stack<>();
for (int i = 0; i < chars.length; i++) {
char ch = chars[i];
if (ch == '(' || ch == '[' || ch == '{') {
stack.push(ch);
} else {
if (stack.empty()) return false;
if (ch == ')' && stack.pop() != '(') return false;
if (ch == ']' && stack.pop() != '[') return false;
if (ch == '}' && stack.pop() != '{') return false;
}
}
return stack.empty();
}
}