Whats wrong with this code??? It keeps giving me same run time error "Last executed input : 4"


  • 0
    J

    int** generateMatrix(int n) {

    int count=0;
    int i,R=0,D=n-1,L=n-1,U=0;
    
    if(n==0)
    return NULL;
    
    int **arr=(int**)malloc(n*sizeof(int));
    for(i=0;i<n;i++)
        arr[i]=(int*)malloc(n*sizeof(int));
    
    while(count/n!=n)
    {
        //right
        for(i=U;i<=D;i++)
        {
            arr[R][i]=++count;
        }
        R++;
    
        //down
        for(i=R;i<=L;i++)
        {
            arr[i][D]=++count;
        }
        D--;
    
        //left
        for(i=D;i>=U;i--)
        {
            arr[L][i]=++count;
        }
        L--;
    
        //up
        for(i=L;i>=R;i--)
        {
    
            arr[i][U]=++count;
        }
        U++;
    }
    return arr;
    

    }


Log in to reply
 

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