Leetcode 20. Valid Parentheses
python实现一个栈.
class Solution(object): def isValid(self, s): """ :type s: str :rtype: bool """ if s.strip()=="": return True brackets={')':'(',']':'[','}':'{'} stack=[] for b in s: if b in brackets: tail=stack.pop() if stack else '#' if tail!=brackets[b]: return False else: stack.append(b) return not stack