C++ solution using vector of bool type

vector of bool type stores only 1 bit (not 1 byte) at every index,hence it is space optimized.
Instead of bool if we use any other data type of size more than 1 bit ,it may not accept for space complexity…

void Solution::setZeroes(vector<vector > &A) {
long long int r=A.size(),c=A[0].size(),i,j;
vector row(r,false); //for storing the rows which contain at least one zero…
vector col(c,false); //for storing the columns which contain at least one zero…
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(A[i][j]==0)
{
row[i]=true; //ith row contains a zero…
col[j]=true; //jth col contains a zero…
}
}
}
for(i=0;i<row.size();i++) // setting all the elements in respective rows with zero…
{
if(row[i])
for(j=0;j<c;j++)
A[i][j]=0;
}
for(i=0;i<col.size();i++) // setting all the elements in respective columns with zero…
{
if(col[i])
for(j=0;j<r;j++)
A[j][i]=0;
}

}

Click here to start solving coding interview questions