Simple c code with O(1) extra space, 16ms


  • 0
    T
    void rotate(int nums[], int n, int k) {
    	int temp,index;
    	index = n-k%n;
    	for (int i = 0; i < n; i++)
    	{
    		temp = nums[i];
    		int swapindex = i;
    		while ((swapindex = (swapindex+index)%n) < i)
    			;
    		temp = nums[swapindex];
    		nums[swapindex] = nums[i];
    		nums[i] = temp;
    	}
    }

Log in to reply
 

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