O(n) time and O(n) space


  • 0
    X
    	public static void rotate2(int[] nums, int k) {
    	System.out.println(Arrays.toString(nums));
    	k = k % nums.length;
    	int[] res = new int[nums.length]; //target
    	int offset = nums.length - k;
    	for (int i = 0; i < nums.length; i++) {
    		res[i] = nums[(i + offset) % nums.length];
    	}
    	nums = res;
    	System.out.println(Arrays.toString(nums));
    }

Log in to reply
 

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