-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBracketsisValid20.java
More file actions
53 lines (40 loc) · 1015 Bytes
/
BracketsisValid20.java
File metadata and controls
53 lines (40 loc) · 1015 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package leetcode;
import java.util.Stack;
/**
* Created by solie_h on 2018/11/28.
*/
public class BracketsisValid20 {
public static boolean isValid(String s) {
char b;
char c;
Stack a = new Stack();
if (s.length() == 0) {
return true;
}
a.push(s.charAt(0));
if (s.length() % 2 != 0) {
return false;
}
for (int i = 1; i < s.length(); i++) {
b = s.charAt(i);
if (a.size()==0){
a.push(b);
continue;
}
c = (char) a.lastElement();
if (b == ')' && c == '(' || b == ']' && c == '[' || b == '}' && c == '{') {
a.pop();
} else {
a.push(b);
}
}
if (a.empty()) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
System.out.println(isValid("()[]{}"));
}
}