Array rotate. My AC C code


  • 0
    W

    void reverse(int nums[],int begin,int end)
    {
    while(begin<end)
    {
    nums[begin]=nums[begin]+nums[end];
    nums[end]=nums[begin]-nums[end];
    nums[begin]=nums[begin]-nums[end];
    begin++;end--;
    }
    }
    void rotate(int nums[],int n,int k)
    {
    if(n<k)
    {
    k%=n;
    }else if(n==k)
    return;
    reverse(nums,0,n-k-1);
    reverse(nums,n-k,n-1);
    reverse(nums,0,n-1);
    }`


Log in to reply
 

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