C Solution, in-place


  • 1
    N
    void rotate(int* nums, int numsSize, int k) {
        int i,j=0,tmp1=nums[0],tmp2,f=0;
        for(i=0;i<numsSize;i++){
            tmp2=nums[((j+k)%numsSize)];
            nums[((j+k)%numsSize)]=tmp1;
            tmp1=tmp2;
            j=(j+k)%numsSize;
            if(j==f){              //if in loop,let it start with next index;
                j++;
                f=j;
                tmp1=nums[f];
            }
        }
    }
    

Log in to reply
 

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