方式一:用键值对
brackets = {'}': '{', ')': '(', ']': '['}
bracketsValues, bracketsKeys = brackets.values(), brackets.keys() # 把键值对的键和值放入list
def is_match(str):
lt = []
for c in str:
if c in bracketsValues:
# 左括号入栈
lt.append(c)
elif c in bracketsKeys:
# 右括号,要么栈顶元素出栈,要么匹配失败
if len(lt) == 0 or lt[-1] != brackets[c]:
return False
lt.pop()
return True
print(is_match(input()))