From b6688d7a8949694c2988d48c79f4ebd75d5229ed Mon Sep 17 00:00:00 2001 From: Qianzhengjun <55390356+Qianzhengjun@users.noreply.github.com> Date: Thu, 31 Mar 2022 11:37:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=860844.=20=E6=AF=94?= =?UTF-8?q?=E8=BE=83=E5=90=AB=E9=80=80=E6=A0=BC=E7=9A=84=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E5=8F=8C=E6=8C=87=E9=92=88=E6=96=B9=E6=B3=95=E7=9A=84?= =?UTF-8?q?Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0844.比较含退格的字符串.md | 30 ++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0844.比较含退格的字符串.md b/problems/0844.比较含退格的字符串.md index 00d52e42..0d83d425 100644 --- a/problems/0844.比较含退格的字符串.md +++ b/problems/0844.比较含退格的字符串.md @@ -185,6 +185,36 @@ class Solution { } ``` +双指针: + +```java +class Solution { +public static boolean backspaceCompare(String s, String t) { + char[] sarray = s.toCharArray(); + char[] tarray = t.toCharArray(); + return generate(sarray).equals(generate(tarray)); + } + public static String generate(char[] a){ + int slow = -1; + int fast = 0; + if(a.length == 1){ + return new String(a); + } else{ + for(fast = 0; fast < a.length; fast++){ + if(a[fast] != '#') + a[++slow] = a[fast]; + else{ + if(slow >= 0) + slow--; + } + } + return new String(a,0,slow + 1); + } + } +} +``` + + ### python