The test case seems wrong


  • 0
    S

    My code is like this:

    int gcd(int a, int b){
        if(a<b){
            gcd(b,a);
        }
        if(a%b){
            gcd(b,a%b);
        }
        else{
            return b;
        }
    }
    void rotate(int nums[], int n, int k) {
        int lcm;
        int i,j;
        int temp;
        int gcd_nk;
        if(k==0){
            return;
        }
        if(k>n){
            k = k%n;
        }
        gcd_nk = gcd(n,k);
        lcm = gcd_nk * (n/gcd_nk) * (k/gcd_nk);
        for (j=0; j<lcm/k; j++){
            for(i=j; i<n/i-1; i+=k){
                temp = nums[(i+k)%n];
                nums[(i+k)%n] = nums[j];
                nums[j] = temp;
            }
        }
    }
    

    and the test case is [1], 1. It seems give me the wrong input, however, when I change my function declare as
    void rotate(int nums[] , int k) , I get the compiler error: too many arguments to function ‘rotate’.


  • 1
    S

    The test case in LeetCode is more readable compare to other online judges.

    [1] 1 doesn't mean there are only 2 params. [1] indicates the n=1 and the array has one element 1. The last 1 represents the k.


Log in to reply
 

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