题解 | #括号序列,Python,栈#
括号序列
http://www.nowcoder.com/practice/37548e94a270412c8b9fb85643c8ccc2
从答题规范上,是不是用入栈出栈的思路应该这么写:
def test_kuohao(s): # 函数名是随便起的 Q = [] sdict = {"(": ")", "[": "]", "{": "}",} try: for a in s: if len(Q) > 0 and sdict[Q[-1]] == a: Q.pop() # 出栈 else: Q.append(a) # 入栈 except: # 如果先遇到了右括号,就直接返回FALSE return False if len(Q) > 0: return False else: return True