-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolutionTwo.java
More file actions
34 lines (30 loc) · 874 Bytes
/
SolutionTwo.java
File metadata and controls
34 lines (30 loc) · 874 Bytes
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
34
package leetCode_20;
import java.util.ArrayDeque;
import java.util.Deque;
/**
* @author dimdark
* @date 2017-10-07
* @time 3:30 PM
*/
public class SolutionTwo {
public boolean isValid(String s) {
Deque<Character> stk = new ArrayDeque<Character>();
char[] cs = s.toCharArray();
for (int i = 0; i < cs.length; ++i) {
if (cs[i] == '(' || cs[i] == '[' || cs[i] == '{') {
stk.push(cs[i]);
} else {
if (stk.isEmpty()) {
return false;
}
char ch = stk.peek();
if ((cs[i] == ')' && ch != '(') || (cs[i] == ']' && ch != '[')
|| (cs[i] == '}' && ch != '{')) {
return false;
}
stk.pop();
}
}
return stk.isEmpty();
}
}