8 ms solution in C --O(1) space In place Replacement


  • 3
    S

    void reverse(int *nums , int begin , int end){

    int tmp;
    while(begin<end){
        tmp=nums[begin];
        nums[begin]=nums[end];
        nums[end]=tmp;
        begin++;
        end--;
    }
    

    }

    void rotate(int* nums, int numsSize, int k) {

    if(k==0 || k==numsSize)
        return;
    if(numsSize==0 )
      return;
        
    reverse(nums,0,numsSize-1);
    if(k>numsSize)
        k=k%numsSize;
    if(numsSize-k!=0){
    
    
    reverse(nums,0,k-1);
     reverse(nums,k,numsSize-1);   
    
    }
    

    }


Log in to reply
 

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