美文网首页
2019-08-13

2019-08-13

作者: Eline_569f | 来源:发表于2019-08-13 10:15 被阅读0次

判断括号是否有效

class Solution:

    def isValid(self, s: str) -> bool:

        if s=="":

            return True

        elif s[0]==")" or s[0]=="]" or s[0]=="}":

            return False

        stack = []

        d = {")":"(","]":"[","}":"{"}

        for i in  s:

            if i=="(" or i=="[" or i=="{":

                stack.append(i)

            else:

                if stack==[]:

                    return False

                if stack.pop()!=d[i]:

                    return False

        return stack==[]

相关文章

网友评论

      本文标题:2019-08-13

      本文链接:https://www.haomeiwen.com/subject/iwfvjctx.html