Anyone has C solution of this question?


  • 1
    L

    What's the means of int** columnSizes and int* returnSize?


  • 0
    E

    @leaf2
    I have the same question.
    This int** columnSizes is confusing me!!
    Also, this int** columnSizes cannot be ralloc row size >2 and cause my output failed.
    Beside that, judge provide wrong parameters and keep causing failed.
    r should be 2, c should be 2 as below.
    Last executed input:
    [[1,2,3,4]]
    2
    2
    But, actual output r,c are 4,1...WTH!!
    Stdout:
    int r:4,int c:1
    0_1493798725474_upload-0da274e3-701a-4113-8fff-e01fe91b1fae )


  • 0

    @earnienew-gmail.com The screenshot is helpful, but please also post your whole code as text so it can be copied&pasted&tested.


  • 0

    I wrote and posted a C solution now with some notes about this stuff.


  • 0
    E

    @StefanPochmann Hi, here is my code. I didn't optimize my code yet, but, please, help to find out what's wrong with it.
    For test case, Last executed input:
    [[1,2,3,4]]
    2
    2
    My code can get correct output in test run, but failed in submit and receive parameters:
    numsRowSize=2, numsColSize=2, r=4, c=1
    //--------------------------------------------------------My code----------------------------------------------------------------------------------//

    int** matrixReshape(int** nums, int numsRowSize, int numsColSize, int r, int c, int** columnSizes, int* returnSize) {

    int i,j;
    int arr[numsRowSize*numsColSize];
    int** res=malloc(r*sizeof(int*));
    
    printf("%d,%d,%d,%d;",numsRowSize,numsColSize,r,c);
    if(numsRowSize*numsColSize!=r*c){ 
        *columnSizes=(int**)malloc(numsRowSize*sizeof(int*));
        for(i=0;i<numsRowSize;i++) (*columnSizes)[i]=numsColSize;
        *returnSize=numsRowSize;
        return nums;
    }
    else{
        for(i=0;i<numsRowSize;i++){
            for(j=0;j<numsColSize;j++) arr[i*numsColSize+j]=nums[i][j];
        }
        *columnSizes=(int**)realloc(*columnSizes,r*sizeof(int*));
        for(i=0;i<r;i++){
            res[i]=realloc(res[i],c*sizeof(int));
            (*columnSizes)[i]=c;
            for(j=0;j<c;j++) res[i][j]=arr[i*c+j];
        }
    }
    *returnSize=r;
    return res;
    

    }


  • 1

    @earnienew-gmail.com I'd say in your if-block it doesn't make sense that you create independent data in returnSize and columnSizes but then return nums. It's an odd mix of making a copy and not making a copy. I think you should make a complete independent copy. After checking your result, the judge probably tries to free both its original matrix as well as yours, and if that's the same, that's likely a problem.

    In your else-block, why are you using realloc instead of malloc as specified?


  • 2

    @leaf2 said in Anyone has C solution of this question?:

    What's the means of int** columnSizes and int* returnSize?

    The return-value is a simple pointer. It only tells where the data starts in memory. How is the judge supposed to know where the data ends? For that, you need to write the size into returnSize. That's apparently LeetCode's standard for returning arrays in C. And since the rows are also arrays, you need to do the same for them, i.e., write their sizes into columnSizes. (No idea why it isn't called rowSizes, though).


  • 0
    E

    @StefanPochmann
    Got it!
    Like you mentioned, the problem is realloc in else-block.
    I shouldn't use realloc, but malloc.
    Thanks!!


  • 0
    L

    @StefanPochmann Thank you for your explanation, the key is to construct the return array's datastruct.


Log in to reply
 

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