Running time error,I don't know why my program executes inappropriately


  • 0
    B

    public:
    void setZeroes(vector<vector<int> > &matrix) {
    if(matrix.empty()) return;
    int rownum=matrix.size();
    int colnum=matrix[0].size();
    bool hasZeroRow=false;
    bool hasZeroCol=false;

        for(int i=0;i<rownum;i++)
        {
            if(matrix[i][0]==0)
            {
                hasZeroCol=true;
                break;
            }
        }
        for(int j=0;j<colnum;j++)
        {
            if(matrix[0][j]==0)
            {
                hasZeroRow=true;
                break;
            }
        }
        
        for(int i=1;i<colnum;i++)
        {
            for(int j=1;j<rownum;j++)
            {
                if(matrix[i][j]==0)
                {
                    matrix[i][0]=0;
                    matrix[0][j]=0;
                }
            }
        }
        
        for(int k=1;k<rownum;k++)
        {
            for(int j=1;j<colnum;j++)
            {
                if(matrix[k][0]==0||matrix[0][j]==0)
                {
                    matrix[k][j]=0;
                }
            }
        }
        
        if(hasZeroRow)
        {
            for (int i=0;i<colnum;i++)
            {
                matrix[0][i]=0;
            }
        }
        if(hasZeroCol)
        {
            for (int i=0;i<rownum;i++)
            {
                matrix[i][0]=0;
            }
        }
    }
    

    };


Log in to reply
 

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