Skip to content

Commit 577dfe4

Browse files
committed
feat: add java solution to leetcode problem: No.0020
1 parent b48a4dd commit 577dfe4

File tree

3 files changed

+104
-35
lines changed

3 files changed

+104
-35
lines changed

solution/0000-0099/0020.Valid Parentheses/README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,42 @@
6464
<!-- 这里可写当前语言的特殊实现逻辑 -->
6565

6666
```java
67-
67+
class Solution {
68+
69+
public boolean isValid(String s) {
70+
if (s == null || s == "") {
71+
return true;
72+
}
73+
char[] chars = s.toCharArray();
74+
Stack<Character> helper = new Stack<>();
75+
for (char c : chars) {
76+
if (isLeft(c)) {
77+
helper.push(c);
78+
} else {
79+
if (helper.isEmpty() || !match(helper.pop(), c)) {
80+
return false;
81+
}
82+
}
83+
}
84+
return helper.isEmpty();
85+
}
86+
87+
private boolean isLeft(char c) {
88+
return c == '(' || c == '[' || c == '{';
89+
}
90+
91+
private boolean isRight(char c) {
92+
return c == ')' || c == ']' || c == '}';
93+
}
94+
95+
private boolean match(char left, char right) {
96+
return (
97+
(left == '(' && right == ')') ||
98+
(left == '[' && right == ']') ||
99+
(left == '{' && right == '}')
100+
);
101+
}
102+
}
68103
```
69104

70105
### **...**

solution/0000-0099/0020.Valid Parentheses/README_EN.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,42 @@
8181
### **Java**
8282

8383
```java
84-
84+
class Solution {
85+
86+
public boolean isValid(String s) {
87+
if (s == null || s == "") {
88+
return true;
89+
}
90+
char[] chars = s.toCharArray();
91+
Stack<Character> helper = new Stack<>();
92+
for (char c : chars) {
93+
if (isLeft(c)) {
94+
helper.push(c);
95+
} else {
96+
if (helper.isEmpty() || !match(helper.pop(), c)) {
97+
return false;
98+
}
99+
}
100+
}
101+
return helper.isEmpty();
102+
}
103+
104+
private boolean isLeft(char c) {
105+
return c == '(' || c == '[' || c == '{';
106+
}
107+
108+
private boolean isRight(char c) {
109+
return c == ')' || c == ']' || c == '}';
110+
}
111+
112+
private boolean match(char left, char right) {
113+
return (
114+
(left == '(' && right == ')') ||
115+
(left == '[' && right == ']') ||
116+
(left == '{' && right == '}')
117+
);
118+
}
119+
}
85120
```
86121

87122
### **...**
Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
class Solution {
2-
public boolean isValid(String s) {
3-
if (s == null || s == "") {
4-
return true;
5-
}
6-
char[] chars = s.toCharArray();
7-
int n = chars.length;
82

9-
Stack<Character> stack = new Stack<>();
10-
11-
for (int i = 0; i < n; ++i) {
12-
char a = chars[i];
13-
if (isLeft(a)) {
14-
stack.push(a);
15-
} else {
16-
if (stack.isEmpty() || !isMatch(stack.pop(), a)) {
17-
return false;
18-
}
19-
}
20-
}
21-
22-
return stack.isEmpty();
23-
24-
}
25-
26-
private boolean isMatch(char a, char b) {
27-
return (a == '(' && b == ')') || (a == '[' && b == ']') || (a == '{' && b == '}');
28-
}
29-
30-
private boolean isLeft(char a) {
31-
return a == '(' || a == '[' || a == '{';
3+
public boolean isValid(String s) {
4+
if (s == null || s == "") {
5+
return true;
326
}
33-
34-
private boolean isRight(char a) {
35-
return a == ')' || a == ']' || a == '}';
7+
char[] chars = s.toCharArray();
8+
Stack<Character> helper = new Stack<>();
9+
for (char c : chars) {
10+
if (isLeft(c)) {
11+
helper.push(c);
12+
} else {
13+
if (helper.isEmpty() || !match(helper.pop(), c)) {
14+
return false;
15+
}
16+
}
3617
}
37-
}
18+
return helper.isEmpty();
19+
}
20+
21+
private boolean isLeft(char c) {
22+
return c == '(' || c == '[' || c == '{';
23+
}
24+
25+
private boolean isRight(char c) {
26+
return c == ')' || c == ']' || c == '}';
27+
}
28+
29+
private boolean match(char left, char right) {
30+
return (
31+
(left == '(' && right == ')') ||
32+
(left == '[' && right == ']') ||
33+
(left == '{' && right == '}')
34+
);
35+
}
36+
}

0 commit comments

Comments
 (0)