LeetCode:Set Matrix Zeros
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
1 class Solution { 2 public: 3 void setZeroes(vector<vector<int>>& matrix) { 4 5 int m=matrix.size(); 6 int n=matrix[0].size(); 7 vector<bool> row(m,false); 8 vector<bool> col(n,false); 9 10 for(int i=0;i<m;++i) 11 for(int j=0;j<n;++j) 12 if(matrix[i][j]==0) 13 row[i]=col[j]=true; 14 15 for(int i=0;i<m;++i) 16 if(row[i]) 17 fill(&matrix[i][0],&matrix[i][0]+n,0); 18 19 for(int j=0;j<n;++j) 20 if(col[j]) 21 for(int i=0;i<m;++i) 22 matrix[i][j]=0; 23 } 24 };