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

 

posted @ 2019-03-12 04:50  周洋  阅读(92)  评论(0编辑  收藏  举报