class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
list1=[]
tuple1=('{}','()','[]')
for i in s:
if i in ('{','[','('):
list1.append(i)
if i in ('}',']',')'):
if list1==[]:
return False
a=list1.pop()
if a+i in tuple1:
pass
else:
return False
if list1==[]:
return True
else:
return False
重点:
- 使用数组作为栈,append(i)&pop()
- 有两处需要判断list是否为空
网友评论