Skip to content

Commit

Permalink
Create: 0189-rotate-array.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
loczek committed Dec 30, 2022
1 parent 2d0e0cc commit 4346096
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions typescript/0189-rotate-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
Do not return anything, modify nums in-place instead.
*/
function rotate(nums: number[], k: number): void {
k = k % nums.length;
let l = 0;
let r = nums.length - 1;
while (l < r) {
let temp = nums[l];
nums[l] = nums[r];
nums[r] = temp;
l += 1;
r -= 1;
}

l = 0;
r = k - 1;
while (l < r) {
let temp = nums[l];
nums[l] = nums[r];
nums[r] = temp;
l += 1;
r -= 1;
}

l = k;
r = nums.length - 1;
while (l < r) {
let temp = nums[l];
nums[l] = nums[r];
nums[r] = temp;
l += 1;
r -= 1;
}
}

0 comments on commit 4346096

Please sign in to comment.