mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
添加(剑指Offer58-II.左旋转字符串.md):增加typescript版本
This commit is contained in:
@ -209,6 +209,31 @@ var reverseLeftWords = function(s, n) {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
|
||||
```typescript
|
||||
function reverseLeftWords(s: string, n: number): string {
|
||||
/** Utils */
|
||||
function reverseWords(strArr: string[], start: number, end: number): void {
|
||||
let temp: string;
|
||||
while (start < end) {
|
||||
temp = strArr[start];
|
||||
strArr[start] = strArr[end];
|
||||
strArr[end] = temp;
|
||||
start++;
|
||||
end--;
|
||||
}
|
||||
}
|
||||
/** Main code */
|
||||
let strArr: string[] = s.split('');
|
||||
let length: number = strArr.length;
|
||||
reverseWords(strArr, 0, length - 1);
|
||||
reverseWords(strArr, 0, length - n - 1);
|
||||
reverseWords(strArr, length - n, length - 1);
|
||||
return strArr.join('');
|
||||
};
|
||||
```
|
||||
|
||||
Swift:
|
||||
|
||||
```swift
|
||||
|
Reference in New Issue
Block a user