add leetcode 20
This commit is contained in:
parent
fd3c25b7da
commit
b564fc563c
26
leetcode/20.py
Normal file
26
leetcode/20.py
Normal file
@ -0,0 +1,26 @@
|
||||
# give a string s containing just '(', ')' and else
|
||||
# determine if the input is valid
|
||||
|
||||
|
||||
class Solution:
|
||||
def isValid(self, s: str) -> bool:
|
||||
symbol_dict = {"(": ")", "{": "}", "[": "]"}
|
||||
stack = []
|
||||
for i in s:
|
||||
try:
|
||||
stack_top = stack.pop()
|
||||
except IndexError:
|
||||
continue
|
||||
|
||||
try:
|
||||
if symbol_dict[stack_top] == i:
|
||||
continue
|
||||
except KeyError:
|
||||
return False
|
||||
|
||||
stack.append(i)
|
||||
|
||||
if stack:
|
||||
return False
|
||||
|
||||
return True
|
Loading…
x
Reference in New Issue
Block a user