Skip to content

Commit 43ff3d9

Browse files
committed
feat: add python solution to leetcode problem: No.0020
1 parent 577dfe4 commit 43ff3d9

File tree

4 files changed

+99
-141
lines changed

4 files changed

+99
-141
lines changed

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

+34-33
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,18 @@
5656
<!-- 这里可写当前语言的特殊实现逻辑 -->
5757

5858
```python
59-
59+
class Solution:
60+
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
6071
```
6172

6273
### **Java**
@@ -65,40 +76,30 @@
6576

6677
```java
6778
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;
79+
public boolean isValid(String s) {
80+
if (s == null || s == "") {
81+
return true;
8182
}
82-
}
83+
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+
}
94+
}
95+
return helper.isEmpty();
96+
}
97+
98+
private boolean match(char left, char right) {
99+
return (left == '(' && right == ')')
100+
|| (left == '[' && right == ']')
101+
|| (left == '{' && right == '}');
83102
}
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-
}
102103
}
103104
```
104105

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

+34-33
Original file line numberDiff line numberDiff line change
@@ -75,47 +75,48 @@
7575
### **Python3**
7676

7777
```python
78-
78+
class Solution:
79+
def isValid(self, s: str) -> bool:
80+
if not s:
81+
return True
82+
helper = []
83+
for c in s:
84+
if c in '([{':
85+
helper.append(c)
86+
else:
87+
if len(helper) == 0 or (helper.pop() + c) not in ["()", "[]", "{}"]:
88+
return False
89+
return len(helper) == 0
7990
```
8091

8192
### **Java**
8293

8394
```java
8495
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;
96+
public boolean isValid(String s) {
97+
if (s == null || s == "") {
98+
return true;
9899
}
99-
}
100+
char[] chars = s.toCharArray();
101+
Stack<Character> helper = new Stack<>();
102+
for (char c : chars) {
103+
boolean isLeft = c == '(' || c == '[' || c == '{';
104+
if (isLeft) {
105+
helper.push(c);
106+
} else {
107+
if (helper.isEmpty() || !match(helper.pop(), c)) {
108+
return false;
109+
}
110+
}
111+
}
112+
return helper.isEmpty();
113+
}
114+
115+
private boolean match(char left, char right) {
116+
return (left == '(' && right == ')')
117+
|| (left == '[' && right == ']')
118+
|| (left == '{' && right == '}');
100119
}
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-
}
119120
}
120121
```
121122

Original file line numberDiff line numberDiff line change
@@ -1,36 +1,26 @@
11
class Solution {
2-
3-
public boolean isValid(String s) {
4-
if (s == null || s == "") {
5-
return true;
6-
}
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;
2+
public boolean isValid(String s) {
3+
if (s == null || s == "") {
4+
return true;
5+
}
6+
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+
}
1517
}
16-
}
18+
return helper.isEmpty();
1719
}
18-
return helper.isEmpty();
19-
}
2020

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-
}
21+
private boolean match(char left, char right) {
22+
return (left == '(' && right == ')')
23+
|| (left == '[' && right == ']')
24+
|| (left == '{' && right == '}');
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,12 @@
11
class Solution:
2-
def isValid(self, s):
3-
"""
4-
:type s: str
5-
:rtype: bool
6-
"""
7-
a="()"
8-
b="[]"
9-
c="{}"
10-
f=False
11-
if s == '':
2+
def isValid(self, s: str) -> bool:
3+
if not s:
124
return True
13-
while s != '':
14-
s=s.replace(a,'')
15-
s=s.replace(b,'')
16-
s=s.replace(c,'')
17-
if s == '':
18-
f=True
19-
return f
20-
if (a in s) or (b in s) or (c in s):
21-
pass
5+
helper = []
6+
for c in s:
7+
if c in '([{':
8+
helper.append(c)
229
else:
23-
f=False
24-
return f
25-
26-
class Solution():
27-
def isValid(self,s):
28-
"""
29-
:type s: str
30-
:rtype: bool
31-
"""
32-
left=['(','{','[']
33-
right={ ')':'(',
34-
']':'[',
35-
'}':'{' }
36-
stack=[]
37-
for i in s:
38-
if i in left:
39-
stack.append(i)
40-
elif stack and right[i] == stack.pop():
41-
continue
42-
else:
43-
return False
44-
if stack:
45-
return False
46-
return True
10+
if len(helper) == 0 or (helper.pop() + c) not in ["()", "[]", "{}"]:
11+
return False
12+
return len(helper) == 0

0 commit comments

Comments
 (0)