Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 38caf52

Browse files
Merge pull request cy69855522#18 from Lebhoryi/master
更新 20 valid paratheses 的 暴力求解法
2 parents 0c9831d + 8cdac46 commit 38caf52

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

‎README.md‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,15 @@ class Solution:
256256
def isValid(self, s: str) -> bool:
257257
while any(('()' in s, '[]' in s, '{}' in s)): s = s.replace('()', '').replace('[]', '').replace('{}', '')
258258
return not s
259+
260+
# 国际站上有一种写法是这样的,相比于上面,下面的写法更加优雅(好理解)一点
261+
class Solution:
262+
def isValid(self, s: str) -> bool:
263+
while s:
264+
l = len(s)
265+
s = s.replace('()', '').replace('[]', '').replace('{}', '')
266+
if l == len(s): break
267+
return not s
259268
```
260269
- 不断删除有效括号直到不能删除,思路简单效率低。另外,stack的方法也很简单,而且快多了。
261270

@@ -264,7 +273,7 @@ class Solution:
264273
def isValid(self, s: str) -> bool:
265274
stack, d = [], {'{': '}', '[': ']', '(': ')'}
266275
for p in s:
267-
if p in '{[(':
276+
if p in d:
268277
stack += [p];
269278
elif not (stack and d[stack.pop()] == p):
270279
return False

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /