添加(1047.删除字符串中的所有相邻重复项.md):增加typescript版本

This commit is contained in:
Steve2020
2022-01-22 17:45:40 +08:00
parent 451fbe16f6
commit 2f6aa72964

View File

@ -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){
//求出字符串长度