Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int rows = matrix.size(), cols = 0;
if(rows == 0) return ;
cols = matrix[0].size();
set<int> rowNum;
set<int> colNum;
for(int i=0; i < rows; i++) {
for(int j=0; j < cols; j++) {
if(matrix[i][j] == 0) {
rowNum.insert(i);
colNum.insert(j);
}
}
}
set<int>::iterator itr;
for(itr=rowNum.begin(); itr!=rowNum.end(); itr++) {
int temp = *itr;
for(int i = 0; i < cols; i++)
matrix[temp][i] = 0;
}
for(itr=colNum.begin(); itr!=colNum.end(); itr++) {
int temp = *itr;
for(int i=0; i < rows; i++) {
matrix[i][temp] = 0;
}
}
}
};