C Question , Why return matrix always blank?


  • 0
    H
    /**
     * Return an array of arrays of size *returnSize.
     * The sizes of the arrays are returned as *columnSizes array.
     * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
     */
    
    int** updateMatrix(int** matrix, int matrixRowSize, int *matrixColSizes, int** columnSizes, int* returnSize) {
           int **return_matrix = (int**)malloc(sizeof(int*) * 3);
       for(int k = 0; k < 3; k++)
       {
          return_matrix[k] = (int *)malloc(sizeof(int) * (3));
          *(*(return_matrix+k)) = 1;
          *((*(return_matrix+k))+1) = 1;
          *((*(return_matrix+k))+1) = 1;
       } 
       *matrixColSizes = 3;
       *(matrixColSizes+1) = 3;
       *(matrixColSizes+2) = 10;
       *returnSize = 3;
    //   returnSize[1] = 3;
    //   returnSize[2] = 3;
       
       
    //   return_matrix[1][1] = 1;
       return matrix;
    }
    

    Your input

    [[0,0,0],[0,1,0],[0,0,0]]

    Your answer

    [[],[],[]]


  • 0
    F

    It looks like the default code definition by LeetCode changes over time.

    In my case, on 30 Aug 2017, it is

    int** updateMatrix(int** matrix, int matrixRowSize, int matrixColSize, int** columnSizes, int* returnSize)
    

    Note that int *matrixColSizes in your case now changes to int matrixColSize in my case.

    Then again, if each row of the matrix has the same column size, it seems meaningless to have int** columnSizes and int* returnSize.

    My submission is accepted anyway. The relevant part (memory allocation) in my code is

        *returnSize = matrixRowSize;
        *columnSizes = (int *)calloc(matrixRowSize, sizeof(int));
        
        int **dist = (int **)calloc(matrixRowSize, sizeof(int *));
        for (int i = 0; i < matrixRowSize; ++i) {
            dist[i] = (int *)calloc(matrixColSize, sizeof(int));
            (*columnSizes)[i] = matrixColSize;
        }
    

    Hope this helps.


Log in to reply
 

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