0073.cpp (644B)
1 class Solution { 2 public: 3 void setZeroes(vector<vector<int>> &matrix) { 4 int n = matrix.size(), m = matrix[0].size(); 5 unordered_set<int> rows, cols; 6 7 for (int i = 0; i < n; i++) { 8 for (int j = 0; j < m; j++) { 9 if (matrix[i][j]) continue; 10 rows.insert(i); 11 cols.insert(j); 12 } 13 } 14 15 for (int row : rows) { 16 for (int j = 0; j < m; j++) 17 matrix[row][j] = 0; 18 } 19 20 for (int col : cols) { 21 for (int i = 0; i < n; i++) 22 matrix[i][col] = 0; 23 } 24 25 return; 26 } 27 };