C++ O(M*N) easy solution using two vectors for marking


#1

void Solution::setZeroes(vector<vector > &A) {
vectorrowmark(A.size(),1);
vectorcolmark(A[0].size(),1);

for(int i=0;i<A.size();i++)
{
for(int j=0;j<A[0].size();j++)
{
if(A[i][j]==0)
{
rowmark[i]=0;
colmark[j]=0;
}
}
}

for(int i=0;i<A.size();i++)
{
if(rowmark[i]==0)
{
for(int j=0;j<A[0].size();j++)
{
A[i][j]=0;
}
}
}
for(int i=0;i<A[0].size();i++)
{
if(colmark[i]==0)
{
for(int j=0;j<A.size();j++)
{
A[j][i]=0;
}
}
}

}

Here we use two vectors rowmark and colmark to mark if that row or column has 0. Then we iterate that vector to see if there is 0 and assign 0 to all the positions in that row or column. Hope this helps!!
refer this video:https://www.youtube.com/watch?v=wEn0y020LA0