[LeetCode] 73. Set Matrix Zeroes
【思路】将每一个(0,0)点坐标存入multimap中,再在遍历时将行和列清零,代码如下:
1 class Solution 2 { 3 public: 4 void setRowCol(vector<vector<int>>& matrix, multimap<int, int> Map) 5 { 6 multimap<int, int>::iterator iter = Map.begin(); 7 for(; iter != Map.end(); iter ++) 8 { 9 for(int i = 0;i < matrix.size(); i ++) 10 { 11 matrix[i][iter->second] = 0;//列清零 12 } 13 for(int j = 0;j < matrix[0].size();j ++) 14 { 15 matrix[iter->first][j] = 0;//行清零 16 } 17 } 18 } 19 void setZeroes(vector<vector<int>>& matrix) 20 { 21 multimap<int, int> Map; 22 for(int i = 0; i < matrix.size(); i ++) 23 { 24 for(int j = 0; j < matrix[0].size(); j ++) 25 { 26 if(matrix[i][j] == 0) 27 { 28 Map.insert(pair<int, int> (i,j)); 29 } 30 } 31 } 32 setRowCol(matrix,Map); 33 } 34 };