Javascript O(1) space solution


  • 0
    R
    var rotate = function (nums, k) {
        const numsLen = nums.length;
        let opTimes = k % numsLen;
        if (opTimes !== 0) {
            if (opTimes > numsLen / 2) {
                opTimes = numsLen - opTimes;
                for (let i = 0; i < opTimes; i++) {
                    nums.push(nums.shift());
                }
            } else {
                for (let i = 0; i < opTimes; i++) {
                    nums.unshift(nums.pop());
                }
            }
        }
    };
    

Log in to reply
 

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