Skip to content

Commit 29a9be4

Browse files
committed
committed from zkp
1 parent f33a26f commit 29a9be4

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

LeetCode/isValid.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def isValid(self, s):
3+
"""
4+
:type s: str
5+
:rtype: bool
6+
"""
7+
# 关键要知道它最近一次匹配的是谁 所以用到栈
8+
stack = []
9+
dick = {')':"(", '}':'{', ']':'['}
10+
for i in s:
11+
if i not in dick:
12+
stack.append(i)
13+
else:
14+
if stack and dick[i] == stack[-1]:
15+
stack.pop()
16+
else:
17+
return False
18+
return not stack

0 commit comments

Comments
 (0)