标签:匹配 pre false values for turn else ack div
判断字符串里面的括号是否是成对出现
SYMBOLS = {‘}‘: ‘{‘, ‘]‘: ‘[‘, ‘)‘: ‘(‘, ‘>‘: ‘<‘} SYMBOLS_L, SYMBOLS_R = SYMBOLS.values(), SYMBOLS.keys() def check(s): stack = [] for c in s: if c in SYMBOLS_L: # 左符号入栈 stack.append(c) elif c in SYMBOLS_R: # 右符号要么出栈,要么匹配失败 if stack and stack[-1] == SYMBOLS[c]: stack.pop() else: return False return True print(check("3 * {3 +[(2 -3) * (4+5)]}")) print(check("3 * {3+ [4 - 6}]"))
标签:匹配 pre false values for turn else ack div
原文地址:https://www.cnblogs.com/doupi/p/13093732.html