1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| class Solution { public: void setZeroes(vector<vector<int>>& matrix) { if (matrix.empty() || matrix[0].empty()) return; int n = matrix.size(), m = matrix[0].size(); bool row0 = false, col0 = false; for (int c = 0; c < m; ++c) { if (matrix[0][c] == 0) { row0 = true; break; } } for (int r = 0; r < n; ++r) { if (matrix[r][0] == 0) { col0 = true; break; } } for (int r = 1; r < n; ++r) { for (int c = 1; c < m; ++c) { if (matrix[r][c] == 0) { matrix[r][0] = matrix[0][c] = 0; } } } for (int r = 1; r < n; ++r) { for (int c = 1; c < m; ++c) { if (matrix[r][0] == 0 || matrix[0][c] == 0) { matrix[r][c] = 0; } } } if (row0) { for (int c = 0; c < m; ++c) { matrix[0][c] = 0; } } if (col0) { for (int r = 0; r < n; ++r) { matrix[r][0] = 0; } } } };
|