-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathExample01_D.java
More file actions
91 lines (79 loc) · 2.45 KB
/
Example01_D.java
File metadata and controls
91 lines (79 loc) · 2.45 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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.util.*;
class Solution {
public boolean isValid(String s) {
// 当字符串本来就是空的时候,我们可以快速返回true
if (s == null || s.length() == 0) {
return true;
}
// 当字符串长度为奇数的时候,不可能是一个有效的合法字符串
if (s.length() % 2 == 1) {
return false;
}
// 消除法的主要核心逻辑:
int leftBraceNumber = 0;
for (int i = 0; i < s.length(); i++) {
// 取出字符
char c = s.charAt(i);
if (c == '(') {
// 如果是'(',那么压栈
leftBraceNumber++;
} else if (c == ')') {
// 如果是')',那么就尝试弹栈
if (leftBraceNumber <= 0) {
// 如果弹栈失败,那么返回false
return false;
}
--leftBraceNumber;
}
}
return leftBraceNumber == 0;
}
}
class UnitTest {
private static Solution solution = new Solution();
public static void testEmptyString() {
assert solution.isValid(null);
assert solution.isValid("");
}
public static void testSingleChar() {
assert !solution.isValid("(");
assert !solution.isValid(")");
}
public static void testTwoChars() {
assert solution.isValid("()");
assert !solution.isValid("((");
assert !solution.isValid("))");
assert !solution.isValid(")(");
}
public static void test3Chars() {
assert !solution.isValid("())");
assert !solution.isValid("(((");
assert !solution.isValid(")))");
assert !solution.isValid(")()");
}
public static void test4Chars() {
assert solution.isValid("()()");
assert solution.isValid("(())");
assert !solution.isValid("))((");
}
public static void testOther() {
assert solution.isValid("()()()");
assert solution.isValid("((()))");
assert solution.isValid("()(())");
assert !solution.isValid("()(()(");
}
public static void run() {
testEmptyString();
testSingleChar();
testTwoChars();
test3Chars();
test4Chars();
testOther();
System.out.println("test over");
}
}
public class Example01_D {
public static void main(String[] argv) {
UnitTest.run();
}
}