Skip to content

Commit 4346096

Browse files
committed
Create: 0189-rotate-array.ts
1 parent 2d0e0cc commit 4346096

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

typescript/0189-rotate-array.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
Do not return anything, modify nums in-place instead.
3+
*/
4+
function rotate(nums: number[], k: number): void {
5+
k = k % nums.length;
6+
let l = 0;
7+
let r = nums.length - 1;
8+
while (l < r) {
9+
let temp = nums[l];
10+
nums[l] = nums[r];
11+
nums[r] = temp;
12+
l += 1;
13+
r -= 1;
14+
}
15+
16+
l = 0;
17+
r = k - 1;
18+
while (l < r) {
19+
let temp = nums[l];
20+
nums[l] = nums[r];
21+
nums[r] = temp;
22+
l += 1;
23+
r -= 1;
24+
}
25+
26+
l = k;
27+
r = nums.length - 1;
28+
while (l < r) {
29+
let temp = nums[l];
30+
nums[l] = nums[r];
31+
nums[r] = temp;
32+
l += 1;
33+
r -= 1;
34+
}
35+
}

0 commit comments

Comments
 (0)