C++ Basic Solution O(m*n) and space complexity of O(m+n)


#1

void Solution::setZeroes(vector<vector > &A) {

// Take two array each for row and column in which we would store which row and coloumn to make zero
vector<int> row(A.size(),1);
vector<int> col(A[0].size(),1);

//Now iterate over matrix
for(int i=0;i<A.size();i++){
    for(int j=0;j<A[0].size();j++){
        if(A[i][j]==0){
            row[i]=0;
            col[j]=0;
        }
    }
}

//Now using both array make changes in A[][]
for(int i=0;i<A.size();i++){
    for(int j=0;j<A[0].size();j++){
        if(row[i]==0||col[j]==0) A[i][j]=0;
    }
}
return;

}