Simple JS in place array shifting


  • 0
    J

    Pop the tail off the array, push it to the front.

    /**
     * @param {number[]} nums
     * @param {number} k
     * @return {void} Do not return anything, modify nums in-place instead.
     */
    var rotate = function(nums, k) {
        for(let i = 0; i < k; i++) {
            let thisNum = nums.pop();
            nums.unshift(thisNum)
        }
        
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.