Five line java solution, what is the time and space complexity?


  • 0
    X
    public void rotate(int[] nums, int k) {
        k = k % nums.length;
        int[] head = Arrays.copyOfRange(nums, 0, nums.length - k);
        int[] tail = Arrays.copyOfRange(nums, nums.length - k, nums.length);
        System.arraycopy(tail, 0, nums, 0, tail.length);
        System.arraycopy(head, 0, nums, k, head.length);
    }

  • 0
    M

    O(n) for both. Your solution would run into problems if nums was null or empty.


Log in to reply
 

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