Three times reverse C 8ms


  • 1
    D
    void rotate(int* nums, int numsSize, int k)
    {
        k=k%numsSize;
        arr_reverse(nums,numsSize-k,numsSize-1);
        arr_reverse(nums,0,numsSize-k-1);
        arr_reverse(nums,0,numsSize-1);
    
    }
    
    void arr_reverse(int *nums,int i,int j)
    {
        int temp;
        while(i<j)
        {
            temp=nums[j];
            nums[j]=nums[i];
            nums[i]=temp;
            i++;
            j--;
        }
    
    }

  • 0
    T

    I came up with this solution today and was surprised that I never thought of this. :D Immediately opened the discuss section if anybody has thought the same. I love this solution.


Log in to reply
 

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