O(1) space, O(m*n) time CPP Solution


#1
void Solution::setZeroes(vector<vector<int> > &A) {
int m = A.size(), n = A[0].size(), temp = 0;

for (int i = 0; i < m ; i++) 
    temp += A[i][0];

for (int i = 0; i < m; i++) 
    for (int j = 1; j < n; j++) 
        A[i][0] += A[i][j];

for (int i = 1; i < n; i++) 
    for (int j = 1; j < m; j++) 
        A[0][i] += A[j][i];

for (int i = 1; i < m; i ++) {
    if (A[i][0] < n) {
        for (int j = 0; j < n; j++) 
            A[i][j] = 0;
    }
    else
        A[i][0] = 1;
}

for (int i = 1; i < n; i ++) {
    if (A[0][i] < m) 
        for (int j = 0; j < m; j++) 
            A[j][i] = 0;
    else
        A[0][i] = 1;
}

if (A[0][0] < n ) 
    for (int i = 0; i < n ;i++) 
        A[0][i] = 0;
else 
    A[0][0] = 1;

if (temp < m)
    for (int i = 0; i < m ;i++) 
        A[i][0] = 0;

return;
}