Easy to read Java solution without using reverse


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

Log in to reply
 

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