C code O(1)space O(n)time 8ms , Is there any suggestion?


  • 0
    D
    void rotate(int* nums, int numsSize, int k) {
        k = k % numsSize;
        if(k == 0)
            return;
        int tmp = 0,index = 0,index2 = numsSize-k,n = numsSize - 1;
        while(n-- > 0){
            if(index == index2 ){ // AVOID CIRCLE LOOP
                index2++; 
                index = index2+k-numsSize;
                n--;
            }
            tmp = nums[index2];   // SAWP
            nums[index2] = nums[index];
            nums[index] = tmp;
            index = (index + k) %  numsSize; // NEW INDEX TO PROCESS
        }
    
     }

Log in to reply
 

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