From 2f6aa72964593420158bf8e4c393ea56ed053dd6 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 22 Jan 2022 17:45:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=881047.=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.md?= =?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...除字符串中的所有相邻重复项.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index d6eefd07..9a0bb1c1 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -267,8 +267,32 @@ var removeDuplicates = function(s) { }; ``` +TypeScript: + +```typescript +function removeDuplicates(s: string): string { + const helperStack: string[] = []; + let i: number = 0; + while (i < s.length) { + let top: string = helperStack[helperStack.length - 1]; + if (top === s[i]) { + helperStack.pop(); + } else { + helperStack.push(s[i]); + } + i++; + } + let res: string = ''; + while (helperStack.length > 0) { + res = helperStack.pop() + res; + } + return res; +}; +``` + C: 方法一:使用栈 + ```c char * removeDuplicates(char * s){ //求出字符串长度