Merge pull request #1051 from xiaofei-2020/stack5

添加(1047.删除字符串中的所有相邻重复项.md):增加typescript版本
This commit is contained in:
程序员Carl
2022-02-08 10:44:59 +08:00
committed by GitHub

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