題目:
Given a string containing just the characters '(', ')', '{', '}', '[' and
']', determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
code:
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
stack=list()
length=len(s)
if length%2!=0:
return False
else:
for i in range(length):
if "(" or "{" or "[" == s[i]:
stack.append(s[i])
elif ")" == s[i]:
if stack.pop() != "(":
return False
elif "}" == s[i]:
if stack.pop() != "{":
return False
elif "]" == s[i]:
if stack.pop() != "[":
return False
if len(stack)!=0:
return False
else:
return True
input是"()"會跑出False
不知道哪裡出了問題