diff --git a/solution/1000-1099/1047.Remove All Adjacent Duplicates In String/README.md b/solution/1000-1099/1047.Remove All Adjacent Duplicates In String/README.md index 406ed6432cea0..e1ebd5c006c0f 100644 --- a/solution/1000-1099/1047.Remove All Adjacent Duplicates In String/README.md +++ b/solution/1000-1099/1047.Remove All Adjacent Duplicates In String/README.md @@ -34,6 +34,12 @@ +栈实现。 + +遍历字符串 S 中的每个字符 s,若栈为空或者栈顶值不等于字符 s,s 入栈,否则栈顶元素出栈。 + +最后返回栈中元素所组成的字符串。 + ### **Python3** @@ -41,7 +47,15 @@ ```python - +class Solution: + def removeDuplicates(self, S: str) -> str: + res = [] + for s in S: + if not res or res[-1] != s: + res.append(s) + else: + res.pop() + return ''.join(res) ``` ### **Java** @@ -49,7 +63,23 @@ ```java - +class Solution { + public String removeDuplicates(String S) { + StringBuilder sb = new StringBuilder(); + int top = -1; + for (int i = 0, n = S.length(); i < n; ++i) { + char s = S.charAt(i); + if (top == -1 || sb.charAt(top) != s) { + sb.append(s); + ++top; + } else { + sb.deleteCharAt(top); + --top; + } + } + return sb.toString(); + } +} ``` ### **...** diff --git a/solution/1000-1099/1047.Remove All Adjacent Duplicates In String/README_EN.md b/solution/1000-1099/1047.Remove All Adjacent Duplicates In String/README_EN.md index d3f025f46a68a..02aab31b724f3 100644 --- a/solution/1000-1099/1047.Remove All Adjacent Duplicates In String/README_EN.md +++ b/solution/1000-1099/1047.Remove All Adjacent Duplicates In String/README_EN.md @@ -42,13 +42,37 @@ For example, in "abbaca" we could remove "bb" since the lett ### **Python3** ```python - +class Solution: + def removeDuplicates(self, S: str) -> str: + res = [] + for s in S: + if not res or res[-1] != s: + res.append(s) + else: + res.pop() + return ''.join(res) ``` ### **Java** ```java - +class Solution { + public String removeDuplicates(String S) { + StringBuilder sb = new StringBuilder(); + int top = -1; + for (int i = 0, n = S.length(); i < n; ++i) { + char s = S.charAt(i); + if (top == -1 || sb.charAt(top) != s) { + sb.append(s); + ++top; + } else { + sb.deleteCharAt(top); + --top; + } + } + return sb.toString(); + } +} ``` ### **...** diff --git a/solution/1000-1099/1047.Remove All Adjacent Duplicates In String/Solution.java b/solution/1000-1099/1047.Remove All Adjacent Duplicates In String/Solution.java index f7e4fa79963de..16275af5244ad 100644 --- a/solution/1000-1099/1047.Remove All Adjacent Duplicates In String/Solution.java +++ b/solution/1000-1099/1047.Remove All Adjacent Duplicates In String/Solution.java @@ -1,14 +1,17 @@ class Solution { public String removeDuplicates(String S) { - char[] cs = new char[S.length()]; + StringBuilder sb = new StringBuilder(); int top = -1; - for (char c : S.toCharArray()) { - if (top >= 0 && c == cs[top]) { - --top; + for (int i = 0, n = S.length(); i < n; ++i) { + char s = S.charAt(i); + if (top == -1 || sb.charAt(top) != s) { + sb.append(s); + ++top; } else { - cs[++top] = c; + sb.deleteCharAt(top); + --top; } } - return String.valueOf(cs, 0, top + 1); + return sb.toString(); } -} +} \ No newline at end of file diff --git a/solution/1000-1099/1047.Remove All Adjacent Duplicates In String/Solution.py b/solution/1000-1099/1047.Remove All Adjacent Duplicates In String/Solution.py new file mode 100644 index 0000000000000..2faf3fd10dd7e --- /dev/null +++ b/solution/1000-1099/1047.Remove All Adjacent Duplicates In String/Solution.py @@ -0,0 +1,9 @@ +class Solution: + def removeDuplicates(self, S: str) -> str: + res = [] + for s in S: + if not res or res[-1] != s: + res.append(s) + else: + res.pop() + return ''.join(res)