-
Notifications
You must be signed in to change notification settings - Fork 0
/
20. 有效的括号.java
34 lines (34 loc) · 1 KB
/
20. 有效的括号.java
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
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack();
for (char c : s.toCharArray()) {
switch (c) {
case '(':
stack.push(c);
break;
case '{':
stack.push(c);
break;
case '[':
stack.push(c);
break;
case ')':
if (stack.isEmpty() || stack.pop() != '(') {
return false;
}
break;
case ']':
if (stack.isEmpty() || stack.pop() != '[') {
return false;
}
break;
case '}':
if (stack.isEmpty() || stack.pop() != '{') {
return false;
}
break;
}
}
return stack.isEmpty();
}
}