题解 | #有效括号序列#

有效括号序列

https://www.nowcoder.com/practice/37548e94a270412c8b9fb85643c8ccc2

class Solution:
    def isValid(self, s: str) -> bool:
        # write code here
        if len(s) % 2:
            return False
        ck_stack = []
        ck_dict = {
            "}": "{",
            "]": "[",
            ")": "(",
        }
        for i in s:
            if i not in ck_dict:
                ck_stack.append(i)
            elif not ck_stack or ck_stack[-1] != ck_dict[i]:
                return False
            else:
                ck_stack.pop()
        return not ck_stack

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务