Two line solution in JS


  • 0
    S

    Here's my solution in JavaScript. It's O(1) for space but I'm not sure how the underlying implementation of splice and push affect space complexity in JS. Since JS doesn't use true arrays, there's no need to move array elements and I believe it's O(n) (or better) for time (someone please correct me if I'm wrong).

    var rotate = function(nums, k) {
        k %= nums.length;
        if (k !== 0) [].push.apply(nums, nums.splice(0, nums.length - k));
    };

Log in to reply
 

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