Why does this code fail for matrix = [[0]] ?


  • -1
    R
        class Solution {
    public:
        void setZeroes(vector<vector<int> > &matrix) {
            int m = matrix.size();
            int n = matrix[0].size();
            int *row  = new int[m];
            int *col  = new int[n];
            int i,j;
            for (i= 0;i<=m;i++)
            {
                for (j=0;j<=n;j++)
                if (matrix[i][j] == 0)
                {
                    row[i] = 2;
                    col[j] = 2;
                }
            }
            for (i= 0;i<=m;i++)
            {
                for (j=0;j<=n;j++)
                if ((row[i] == 2)||(col[j] == 2))
                   matrix[i][j] = 0;
            }
        }
    };

  • 0
    T

    This is part of your code

    for (i= 0;i<=m;i++)
            {
                for (j=0;j<=n;j++)
                if (matrix[i][j] == 0) ???
    

    Are you sure this is right?

    matrix[m][n] out of bound?

    I didnot check other parts.


Log in to reply
 

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