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.
1 class Solution(object): 2 def isValid(self, s): 3 """ 4 :type s: str 5 :rtype: bool 6 """ 7 stack=[] 8 dict={')':'(',']':'[','}':'{'} 9 for char in s: 10 if char in dict.values(): 11 stack.append(char) 12 elif char in dict: 13 if stack == [] or dict[char] != stack.pop(): 14 return False 15 else: 16 return False 17 return stack == []
stack 是列表
dict是字典
dict.keys()
dict.values()
stack.append()
stack.pop()
最后stack 中应该是空的。
浙公网安备 33010602011771号