Skip to content

Commit

Permalink
Merge pull request youngyangyang04#1029 from xiaofei-2020/str3
Browse files Browse the repository at this point in the history
添加(剑指Offer05.替换空格.md):增加typescript版本
  • Loading branch information
youngyangyang04 authored Jan 24, 2022
2 parents 78712d3 + 3d7ec66 commit 6010030
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions problems/剑指Offer05.替换空格.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,33 @@ javaScript:
};
```

TypeScript:

```typescript
function replaceSpace(s: string): string {
let arr: string[] = s.split('');
let spaceNum: number = 0;
let oldLength: number = arr.length;
for (let i = 0; i < oldLength; i++) {
if (arr[i] === ' ') {
spaceNum++;
}
}
arr.length = oldLength + 2 * spaceNum;
let cur: number = oldLength - 1;
for (let i = arr.length - 1; i >= 0; i--, cur--) {
if (arr[cur] !== ' ') {
arr[i] = arr[cur]
} else {
arr[i] = '0';
arr[--i] = '2';
arr[--i] = '%';
}
}
return arr.join('');
};
```

Swift:

```swift
Expand Down

0 comments on commit 6010030

Please sign in to comment.