20. 有效的括号

20. 有效的括号

方法一

class Solution(object):
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        d = {'}': '{', ']': '[', ')': '('}
        stack = []
        for c in s:
            if c not in d:
                stack.append(c)
                continue
            if not stack or stack.pop() != d[c]:
                return False
        return False if stack else True

 

posted @ 2019-01-21 10:12  小学弟-  阅读(144)  评论(0编辑  收藏  举报