Java O(2n) 1ms simple solution


  • 0
    S
    public void rotate(int[] nums, int k) {
            k = nums.length-k%nums.length;
            int[] n = new int[nums.length];
            for(int i = 0; i < nums.length; i++){
                n[i] = nums[(i+k)%nums.length];       
            }
            for(int i = 0; i < nums.length; i++){
                nums[i] = n[i];       
            }
        }
    

Log in to reply
 

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