Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.
The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not.
匹配括号
每一个‘(‘都有’)‘匹配 同理[] {}
把遇到的每一个左半边括号压栈 遇到右半边括号pop() 如果所有括号都匹配即合理
class Solution:
# @param {string} s
# @return {boolean}
def isValid(self, s):
l = len(s)
stack = []
dic = { '(':')','[':']','{':'}'}
for i in s:
print stack
if i == '(':
stack.append('(')
continue
if i == '[':
stack.append('[')
continue
if i == '{':
stack.append('{')
continue
if stack == []:
return False
temp = stack.pop()
if i != dic[temp]:
return False
if stack == []:
return True
else:
return False