[C] PC can work well, but Runtime error. it show Runtime Error, Last executed input: [[0]]


  • 0
    T
    void setZeroes(int** matrix, int matrixRowSize, int matrixColSize) {
    int i=0, j=0;
    
    bool row[matrixRowSize];
    bool col[matrixColSize];    
    
    for(i=0; i<matrixRowSize; i++)
    {
        for(j=0; j<matrixColSize; j++)
        {
            if(matrix[i*matrixColSize + j] == 0)
            {
                row[i] = true;
                col[j]= true;
            }
        }
    }
    
    for(i=0; i<matrixRowSize; i++)
    {
        if(row[i] == true)
        {
            for(j=0; j <matrixColSize; j++)
                matrix[i*matrixColSize + j] = 0;
        }            
    }
    
    for(j=0; j<matrixColSize; j++)
    {
        if(col[j] == true)
        {
            for(i=0; i <matrixRowSize; i++)
                matrix[i*matrixColSize + j] = 0;
        }            
    }
    

    }


  • 0
    D

    I've modified your code to make it works as following. Take note where I put the comment "//changed".
    One note about leetcode: when there is runtime error, it may not show the actual input that caused the error, it only shows the "last executed input".

    void setZeroes(int** matrix, int matrixRowSize, int matrixColSize) {
        int i=0, j=0;
        
        bool row[matrixRowSize];
        bool col[matrixColSize];    
        
        for(i=0; i<matrixRowSize; i++) row[i] = false; //changed
        for(j=0; j<matrixColSize; j++) col[j] = false; //changed
        
        for(i=0; i<matrixRowSize; i++)
        {
            for(j=0; j<matrixColSize; j++)
            {
                if(matrix[i][j] == 0) //changed
                {
                    row[i] = true;
                    col[j]= true;
                }
            }
        }
        
        for(i=0; i<matrixRowSize; i++)
        {
            if(row[i] == true)
            {
                for(j=0; j <matrixColSize; j++)
                    matrix[i][j] = 0; //changed
            }            
        }
        
        for(j=0; j<matrixColSize; j++)
        {
            if(col[j] == true)
            {
                for(i=0; i <matrixRowSize; i++)
                    matrix[i][j] = 0; //changed
            }            
        }
        
    }

  • 0
    T

    Thank you for your answer, because of my poor knowledge on two dimensional pointer, I did not know how to tranmit two dimensional array to two dimensional pointer, so I use matrix[i*matrixColSize + j] to do it.


Log in to reply
 

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