string = input() stack = [] for s in string: if stack.__len__()==0: stack.append(s) elif s==stack[-1]: stack.pop() else: stack.append(s) if stack.__len__()==0: print(0) else: print(''.join(stack)) 抄的前面一位大佬的,采用栈的思路,优化了一下代码,让代码更优雅。