O(1) space and o(n) time complexity Java solution


  • 0
    S

    public class RotateArray {

    public void rotate2(int[] nums, int k) {
    	int l = nums.length;
    	k %= l;
    	if (k > 0) {
    		for (int st = 0, current, last = nums[0], swap, step = l; step > 0; ++st) {
    			current = st;
    			do {
    				swap = nums[current];
    				nums[current] = last;
    				last = swap;
    				current = (current + k) % l;
    				--step;
    			} while (current != st);
    			nums[st] = last;
    		}
    	}
    }
    

    }


Log in to reply
 

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