From ac40cc8be29fdbf22b2b6a5b889315de74f602c3 Mon Sep 17 00:00:00 2001 From: KailokFung Date: Wed, 23 Jun 2021 10:24:40 +0800 Subject: [PATCH] =?UTF-8?q?fix(1047):=20=E6=96=B0=E5=A2=9E=E5=8F=8C?= =?UTF-8?q?=E6=8C=87=E9=92=88java=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...除字符串中的所有相邻重复项.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index 305a287d..4f13cda3 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -171,6 +171,29 @@ class Solution { } ``` +拓展:双指针 +```java +class Solution { + public String removeDuplicates(String s) { + char[] ch = s.toCharArray(); + int fast = 0; + int slow = 0; + while(fast < s.length()){ + // 直接用fast指针覆盖slow指针的值 + ch[slow] = ch[fast]; + // 遇到前后相同值的,就跳过,即slow指针后退一步,下次循环就可以直接被覆盖掉了 + if(slow > 0 && ch[slow] == ch[slow - 1]){ + slow--; + }else{ + slow++; + } + fast++; + } + return new String(ch,0,slow); + } +} +``` + Python: ```python3 class Solution: