leetcode 20 有效的括号
简单题,用list模拟栈。
class Solution:
def isValid(self, s: str) -> bool:
stack = []
for i in s:
if i=='(' or i=='[' or i=='{':
stack.append(i)
elif stack==[] and (i==')' or i==']' or i=='}'):
return False
elif i==')' and stack[-1]=='(':
del stack[-1]
elif i==']' and stack[-1]=='[':
del stack[-1]
elif i=='}' and stack[-1]=='{':
del stack[-1]
else:
return False
if stack==[]:
return True
else:
return False
36ms 13.8MB