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 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=860844.=20?= =?UTF-8?q?=E6=AF=94=E8=BE=83=E5=90=AB=E9=80=80=E6=A0=BC=E7=9A=84=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E5=8F=8C=E6=8C=87=E9=92=88=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E7=9A=84Java=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 From d1ea59aebd4659274165c5a352166696275faddc Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Thu, 31 Mar 2022 22:14:20 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880093.=E5=A4=8D?= =?UTF-8?q?=E5=8E=9FIP=E5=9C=B0=E5=9D=80.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0093.复原IP地址.md | 39 +++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/problems/0093.复原IP地址.md b/problems/0093.复原IP地址.md index 714dcb4f..7910fc50 100644 --- a/problems/0093.复原IP地址.md +++ b/problems/0093.复原IP地址.md @@ -455,6 +455,45 @@ var restoreIpAddresses = function(s) { }; ``` +## TypeScript + +```typescript +function isValidIpSegment(str: string): boolean { + let resBool: boolean = true; + let tempVal: number = Number(str); + if ( + str.length === 0 || isNaN(tempVal) || + tempVal > 255 || tempVal < 0 || + (str.length > 1 && str[0] === '0') + ) { + resBool = false; + } + return resBool; +} +function restoreIpAddresses(s: string): string[] { + const resArr: string[] = []; + backTracking(s, 0, []); + return resArr; + function backTracking(s: string, startIndex: number, route: string[]): void { + let length: number = s.length; + if (route.length === 4 && startIndex >= length) { + resArr.push(route.join('.')); + return; + } + if (route.length === 4 || startIndex >= length) return; + let tempStr: string = ''; + for (let i = startIndex + 1; i <= Math.min(length, startIndex + 3); i++) { + tempStr = s.slice(startIndex, i); + if (isValidIpSegment(tempStr)) { + route.push(s.slice(startIndex, i)); + backTracking(s, i, route); + route.pop(); + } + } + } +}; +``` + ## Go 回溯(对于前导 0的IP(特别注意s[startIndex]=='0'的判断,不应该写成s[startIndex]==0,因为s截取出来不是数字))