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
重點:
- 使用數(shù)組作為棧,append(i)&pop()
- 有兩處需要判斷l(xiāng)ist是否為空