Skip to content

Commit 4091c09

Browse files
committed
feat: update solutions to leetcode problem: No.0020
1 parent ed8f1a6 commit 4091c09

File tree

4 files changed

+54
-84
lines changed

4 files changed

+54
-84
lines changed

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

+22-28
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52+
栈实现。
53+
54+
遍历括号字符串,遇到左括号时,将左括号压入栈中;遇到右括号时,弹出栈顶元素(栈若为空,直接返回 false),判断是否是相同类型的括号。若不匹配,直接返回 false。
55+
56+
遍历结束,栈若为空,说明括号字符串有效。
57+
5258
<!-- tabs:start -->
5359

5460
### **Python3**
@@ -58,16 +64,14 @@
5864
```python
5965
class Solution:
6066
def isValid(self, s: str) -> bool:
61-
if not s:
62-
return True
63-
helper = []
64-
for c in s:
65-
if c in '([{':
66-
helper.append(c)
67-
else:
68-
if len(helper) == 0 or (helper.pop() + c) not in ["()", "[]", "{}"]:
69-
return False
70-
return len(helper) == 0
67+
q = []
68+
parentheses = {'()', '[]', '{}'}
69+
for ch in s:
70+
if ch in '([{':
71+
q.append(ch)
72+
elif not q or q.pop() + ch not in parentheses:
73+
return False
74+
return not q
7175
```
7276

7377
### **Java**
@@ -77,28 +81,18 @@ class Solution:
7781
```java
7882
class Solution {
7983
public boolean isValid(String s) {
80-
if (s == null || s == "") {
81-
return true;
82-
}
8384
char[] chars = s.toCharArray();
84-
Stack<Character> helper = new Stack<>();
85-
for (char c : chars) {
86-
boolean isLeft = c == '(' || c == '[' || c == '{';
87-
if (isLeft) {
88-
helper.push(c);
89-
} else {
90-
if (helper.isEmpty() || !match(helper.pop(), c)) {
91-
return false;
92-
}
93-
}
85+
Deque<Character> q = new ArrayDeque<>();
86+
for (char ch : chars) {
87+
boolean left = ch == '(' || ch == '[' || ch == '{';
88+
if (left) q.push(ch);
89+
else if (q.isEmpty() || !match(q.pop(), ch)) return false;
9490
}
95-
return helper.isEmpty();
91+
return q.isEmpty();
9692
}
9793

98-
private boolean match(char left, char right) {
99-
return (left == '(' && right == ')')
100-
|| (left == '[' && right == ']')
101-
|| (left == '{' && right == '}');
94+
private boolean match(char l, char r) {
95+
return (l == '(' && r == ')') || (l == '{' && r == '}') || (l == '[' && r == ']');
10296
}
10397
}
10498
```

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

+16-28
Original file line numberDiff line numberDiff line change
@@ -74,45 +74,33 @@
7474
```python
7575
class Solution:
7676
def isValid(self, s: str) -> bool:
77-
if not s:
78-
return True
79-
helper = []
80-
for c in s:
81-
if c in '([{':
82-
helper.append(c)
83-
else:
84-
if len(helper) == 0 or (helper.pop() + c) not in ["()", "[]", "{}"]:
85-
return False
86-
return len(helper) == 0
77+
q = []
78+
parentheses = {'()', '[]', '{}'}
79+
for ch in s:
80+
if ch in '([{':
81+
q.append(ch)
82+
elif not q or q.pop() + ch not in parentheses:
83+
return False
84+
return not q
8785
```
8886

8987
### **Java**
9088

9189
```java
9290
class Solution {
9391
public boolean isValid(String s) {
94-
if (s == null || s == "") {
95-
return true;
96-
}
9792
char[] chars = s.toCharArray();
98-
Stack<Character> helper = new Stack<>();
99-
for (char c : chars) {
100-
boolean isLeft = c == '(' || c == '[' || c == '{';
101-
if (isLeft) {
102-
helper.push(c);
103-
} else {
104-
if (helper.isEmpty() || !match(helper.pop(), c)) {
105-
return false;
106-
}
107-
}
93+
Deque<Character> q = new ArrayDeque<>();
94+
for (char ch : chars) {
95+
boolean left = ch == '(' || ch == '[' || ch == '{';
96+
if (left) q.push(ch);
97+
else if (q.isEmpty() || !match(q.pop(), ch)) return false;
10898
}
109-
return helper.isEmpty();
99+
return q.isEmpty();
110100
}
111101

112-
private boolean match(char left, char right) {
113-
return (left == '(' && right == ')')
114-
|| (left == '[' && right == ']')
115-
|| (left == '{' && right == '}');
102+
private boolean match(char l, char r) {
103+
return (l == '(' && r == ')') || (l == '{' && r == '}') || (l == '[' && r == ']');
116104
}
117105
}
118106
```
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
class Solution {
22
public boolean isValid(String s) {
3-
if (s == null || s == "") {
4-
return true;
5-
}
63
char[] chars = s.toCharArray();
7-
Stack<Character> helper = new Stack<>();
8-
for (char c : chars) {
9-
boolean isLeft = c == '(' || c == '[' || c == '{';
10-
if (isLeft) {
11-
helper.push(c);
12-
} else {
13-
if (helper.isEmpty() || !match(helper.pop(), c)) {
14-
return false;
15-
}
16-
}
4+
Deque<Character> q = new ArrayDeque<>();
5+
for (char ch : chars) {
6+
boolean left = ch == '(' || ch == '[' || ch == '{';
7+
if (left) q.push(ch);
8+
else if (q.isEmpty() || !match(q.pop(), ch)) return false;
179
}
18-
return helper.isEmpty();
10+
return q.isEmpty();
1911
}
2012

21-
private boolean match(char left, char right) {
22-
return (left == '(' && right == ')')
23-
|| (left == '[' && right == ']')
24-
|| (left == '{' && right == '}');
13+
private boolean match(char l, char r) {
14+
return (l == '(' && r == ')') || (l == '{' && r == '}') || (l == '[' && r == ']');
2515
}
2616
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
class Solution:
22
def isValid(self, s: str) -> bool:
3-
if not s:
4-
return True
5-
helper = []
6-
for c in s:
7-
if c in '([{':
8-
helper.append(c)
9-
else:
10-
if len(helper) == 0 or (helper.pop() + c) not in ["()", "[]", "{}"]:
11-
return False
12-
return len(helper) == 0
3+
q = []
4+
parentheses = {'()', '[]', '{}'}
5+
for ch in s:
6+
if ch in '([{':
7+
q.append(ch)
8+
elif not q or q.pop() + ch not in parentheses:
9+
return False
10+
return not q

0 commit comments

Comments
 (0)