My java solution which is easy to understand


  • 0
    G

    public class Solution {
    public static void rotate(int[] nums, int k) {

    	if(k<=0||nums.length==0) return;
    	k = k%nums.length;
    	rotate(nums);
    	int[] nums1 = new int[k];
    	System.arraycopy(nums, 0, nums1, 0, k);  
    	int[] nums2 = new int[nums.length-k];  
    	System.arraycopy(nums, k, nums2, 0, nums.length-k);
    	rotate(nums1);
    	rotate(nums2);
    	System.arraycopy(nums1, 0, nums, 0, k);
    	System.arraycopy(nums2, 0, nums, k, nums.length-k);
    }
    
    public static void rotate(int[] nums){
    	int length = nums.length;
    	for(int i=0;i<length/2;i++){
    		int temp = nums[i];
    		nums[i] = nums[length-1-i];
    		nums[length-1-i] = temp;
    	}
    }
    

    }


Log in to reply
 

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