From b9515d36d2d46114bae9ba1a2f64a25ba8ac6290 Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Thu, 1 Apr 2021 12:52:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=9B=E6=89=A3=EF=BC=9A1047:=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E6=89=80?= =?UTF-8?q?=E6=9C=89=E7=9B=B8=E9=82=BB=E9=87=8D=E5=A4=8D=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RemoveAllAdjacentDuplicatesInString.java | 62 +++++++++++++++++++ .../cn/RemoveAllAdjacentDuplicatesInString.md | 25 ++++++++ 2 files changed, 87 insertions(+) create mode 100644 LeetCode/src/main/java/leetcode/editor/cn/RemoveAllAdjacentDuplicatesInString.java create mode 100644 LeetCode/src/main/java/leetcode/editor/cn/RemoveAllAdjacentDuplicatesInString.md diff --git a/LeetCode/src/main/java/leetcode/editor/cn/RemoveAllAdjacentDuplicatesInString.java b/LeetCode/src/main/java/leetcode/editor/cn/RemoveAllAdjacentDuplicatesInString.java new file mode 100644 index 0000000..540ae01 --- /dev/null +++ b/LeetCode/src/main/java/leetcode/editor/cn/RemoveAllAdjacentDuplicatesInString.java @@ -0,0 +1,62 @@ +//给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。 +// +// 在 S 上反复执行重复项删除操作,直到无法继续删除。 +// +// 在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。 +// +// +// +// 示例: +// +// 输入:"abbaca" +//输出:"ca" +//解释: +//例如,在 "abbaca" 中,我们可以删除 "bb" 由于两字母相邻且相同,这是此时唯一可以执行删除操作的重复项。之后我们得到字符串 "aaca",其中又 +//只有 "aa" 可以执行重复项删除操作,所以最后的字符串为 "ca"。 +// +// +// +// +// 提示: +// +// +// 1 <= S.length <= 20000 +// S 仅由小写英文字母组成。 +// +// Related Topics 栈 +// 👍 247 👎 0 + +package leetcode.editor.cn; + +import java.util.Stack; + +//1047:删除字符串中的所有相邻重复项 +public class RemoveAllAdjacentDuplicatesInString { + public static void main(String[] args) { + //测试代码 + Solution solution = new RemoveAllAdjacentDuplicatesInString().new Solution(); + } + + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public String removeDuplicates(String S) { + Stack stack = new Stack<>(); + for (char ch : S.toCharArray()) { + if (!stack.isEmpty() && stack.peek() == ch) { + stack.pop(); + } else { + stack.push(ch); + } + } + StringBuilder SBuilder = new StringBuilder(); + while (!stack.isEmpty()) { + SBuilder.insert(0, stack.pop()); + } + S = SBuilder.toString(); + return S; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/LeetCode/src/main/java/leetcode/editor/cn/RemoveAllAdjacentDuplicatesInString.md b/LeetCode/src/main/java/leetcode/editor/cn/RemoveAllAdjacentDuplicatesInString.md new file mode 100644 index 0000000..f9c5626 --- /dev/null +++ b/LeetCode/src/main/java/leetcode/editor/cn/RemoveAllAdjacentDuplicatesInString.md @@ -0,0 +1,25 @@ +

给出由小写字母组成的字符串 S重复项删除操作会选择两个相邻且相同的字母,并删除它们。

+ +

在 S 上反复执行重复项删除操作,直到无法继续删除。

+ +

在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。

+ +

 

+ +

示例:

+ +
输入:"abbaca"
+输出:"ca"
+解释:
+例如,在 "abbaca" 中,我们可以删除 "bb" 由于两字母相邻且相同,这是此时唯一可以执行删除操作的重复项。之后我们得到字符串 "aaca",其中又只有 "aa" 可以执行重复项删除操作,所以最后的字符串为 "ca"。
+
+ +

 

+ +

提示:

+ +
    +
  1. 1 <= S.length <= 20000
  2. +
  3. S 仅由小写英文字母组成。
  4. +
+
Related Topics
  • \n
  • 👍 247
  • 👎 0
  • \ No newline at end of file