题解 | 字符串的相邻字符去重
字符串的相邻字符去重
https://www.nowcoder.com/practice/8c7d55263c624faa8e48a16d92c2e90d
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param s string字符串
# @return string字符串
#
class Solution:
def removeDuplicates(self , s: str) -> str:
# write code here
stack = []#栈
for c in s:
if len(stack)>0 and c==stack[-1]:#要消除的情况,消除后退出本次循环
stack.pop()
continue
stack += [c]#如果本次没消除,则将本次的字符加入栈中
return ''.join(stack)#栈中的元素合并后返回
海康威视公司福利 1407人发布
