原题链接
栈
- 扫描过的字符入栈暂存
- 如果扫描的当前字符与栈顶元素相同,则将栈顶元素出栈
- 将栈中剩余的字符转换成字符串
const removeDuplicates = function(s) {
const stack = []
for (const i of s) {
if (stack.length && stack[stack.length - 1] === i) {
stack.pop()
} else {
stack.push(i)
}
}
return stack.join('')
}