Leetcode刷题记录——20. 有效的括号

在这里插入图片描述

class Solution:
    def isValid(self, s: str) -> bool:
        leftstack = []
        rightstack = []
        left = ['(','[','{']
        right = [')',']','}']
        doubledict = {'(':')','[':']','{':'}'}
        for letter in s:
            if letter in left:
                leftstack.append(letter)
            elif letter in right:
                if leftstack == [] or doubledict[leftstack.pop(-1)] != letter:
                    return False
        if leftstack != []:
            return False
        return True

        
发布了59 篇原创文章 · 获赞 14 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_41545780/article/details/105475583