javascript-leetcode icon indicating copy to clipboard operation
javascript-leetcode copied to clipboard

1047. 删除字符串中的所有相邻重复项

Open Geekhyt opened this issue 4 years ago • 0 comments

原题链接

  1. 扫描过的字符入栈暂存
  2. 如果扫描的当前字符与栈顶元素相同,则将栈顶元素出栈
  3. 将栈中剩余的字符转换成字符串
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('')
}
  • 时间复杂度: O(n)
  • 空间复杂度: O(n)

Geekhyt avatar Sep 19 '21 15:09 Geekhyt