【LeetCode-020】valid parenthesis

1. 问题描述:

2. 解决办法:
 

class Solution(object):
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        dict_match = {"(": ")", "{": "}", "[": "]"}
        left = []
        for i in range(0, len(s), 1):
            if s[i] in dict_match:
                left.append(s[i])
            else:
                if len(left) == 0 or dict_match[left[-1]] != s[i]:
                    return False
                else:
                    left.pop()
        return left == []


s = Solution()
A = s.isValid("()[){}")
print(A)

3. 个人记录
开心,这算是自己做出来的,虽然是简单的题目,哈哈
值得记录的几个点:
if a in dict, 这时候dict会自动生成以它的关键字为组成的列表
本题主要是要用到堆栈的这种数据类型
 

猜你喜欢

转载自blog.csdn.net/zhangjipinggom/article/details/85146083