class Solution:
# @return a boolean
def isValid(self, s):
if len(s)>1:
stack=[]
for words in s:
if len(stack)==0:
stack.append(words)
else:
if words=='(' or words=='[' or words=='{':
stack.append(words)
else:
word=stack[len(stack)-1]
if self.isRighr(word,words):
stack.pop()
else:
return False
if len(stack)==0:
return True
else:
return False
elif len(s)==1:
return False
else:
return True
def isRighr(self,a,b):
if a=='(' and b==')':
return True
if a=='[' and b==']':
return True
if a=='{' and b=='}':
return True
return False
Python学习——leetcode(Valid Parentheses)
最新推荐文章于 2022-08-03 18:37:31 发布