Naive Approach
In this i'm just using an extra array to store
/**
* @param {number[]} nums
* @param {number} k
* @return {void} Do not return anything, modify nums in-place instead.
*/
var rotate = function(nums, k) {
let res = [];
let n = nums.length;
/*for case [1,2] sorting 3 times is equivalent to sorting 1 times*/
k = k % n; //to handle cases where k>n
for(let i = n-k ;in;i++){
res.push(nums[i])
}
for(let i = 0 ;in-k;i++){
res.push(nums[i])
}
for (let i = 0; i n; i++) {
nums[i] = res[i];
}
};
More...
In this i'm just using an extra array to store
/**
* @param {number[]} nums
* @param {number} k
* @return {void} Do not return anything, modify nums in-place instead.
*/
var rotate = function(nums, k) {
let res = [];
let n = nums.length;
/*for case [1,2] sorting 3 times is equivalent to sorting 1 times*/
k = k % n; //to handle cases where k>n
for(let i = n-k ;in;i++){
res.push(nums[i])
}
for(let i = 0 ;in-k;i++){
res.push(nums[i])
}
for (let i = 0; i n; i++) {
nums[i] = res[i];
}
};
More...