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 中应该是空的。

posted on 2017-03-06 13:58  Ci_pea  阅读(100)  评论(0)    收藏  举报