O(n) time and O(n) space


  • 0
    public void rotate(int[] nums, int k) {
            int l=nums.length;
            if(l==0){
                return;
            }
            int[] r=new int[l];
            for(int i=0;i<l;i++){
                r[(i+k)%l]=nums[i];
            }
            for(int i=0;i<l;i++){
                nums[i]=r[i];
            }
        }
    

Log in to reply
 

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