Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1769 from loczek/0189-rotate-array
Browse files Browse the repository at this point in the history
Create: 0189-rotate-array.ts
  • Loading branch information
Ahmad-A0 authored Jan 1, 2023
2 parents 5e690f9 + 4346096 commit 96ecdd5
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 96ecdd5

Please sign in to comment.