python-判断括号是否匹配

BRACKET = {'}': '{', ')': '(', ']': '['}
BRACKET_L, BRACKET_R = BRACKET.values(), BRACKET.keys()


def Check_bracket(s):
    arr = []
    for c in s:
        if c in BRACKET_L:
            # 左括号入栈
            arr.append(c)
        elif c in BRACKET_R:
            # 右括号,要么栈顶元素出栈,要么匹配失败
            if arr and arr[-1] == BRACKET[c]:
                arr.pop()
            else:
                return False
    return True


print(Check_bracket("((()))((()))"))

 

你可能感兴趣的:(python,面试,面试)