Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Geekhyt opened this issue Sep 19, 2021 · 0 comments
Open

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

Geekhyt opened this issue Sep 19, 2021 · 0 comments
Labels

Comments

@Geekhyt
Copy link
Owner

Geekhyt commented Sep 19, 2021

原题链接

  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 Geekhyt added the 简单 label Sep 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant