-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetcode020.java
66 lines (59 loc) · 1.25 KB
/
Leetcode020.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package test;
//有效括号对
public class Leetcode020 {
//判断是否成对
public boolean isPair(char a, char b) {
switch (a) {
case '(': {
if (b == ')')
return true;
}
case '[': {
if (b == ']')
return true;
}
case '{': {
if (b == '}')
return true;
}
}
return false;
}
//判断是否有效字符串
public boolean isValid(String s) {
//奇数个字符必然为假
if (s.length() == 0 || s.length() % 2 == 1)
return false;
//前置括号pre,当遇到不成对左括号写入,当遇到不成对右括号与前置字符串最后一个比较,不成对则为假
int i = 0;
String pre = "";
while (i < s.length()) {
if (i < s.length()-1 && isPair(s.charAt(i), s.charAt(i+1))) {
i += 2;
continue;
}
if (s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) == '{') {
pre += s.charAt(i);
i++;
continue;
}
if (pre.length() > 0 && isPair(pre.charAt(pre.length()-1), s.charAt(i))) {
pre = pre.substring(0, pre.length()-1);
i++;
continue;
} else {
return false;
}
}
//若前置字符串非空,为假,否则为真
if (pre.length() == 0)
return true;
else
return false;
}
public static void main(String argsp[]) {
String s = "([])";
Leetcode020 test = new Leetcode020();
System.out.println(test.isValid(s));
}
}