-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path73SetMatrixZero.cpp
More file actions
39 lines (39 loc) · 1.12 KB
/
73SetMatrixZero.cpp
File metadata and controls
39 lines (39 loc) · 1.12 KB
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
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int rows = matrix.size();
int cols = matrix[0].size();
vector<bool> rowZero(rows, false);
vector<bool> colZero(cols, false);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] == 0) {
rowZero[i] = true;
colZero[j] = true;
}
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (rowZero[i] || colZero[j]) {
matrix[i][j] = 0;
}
}
}
cout << "[";
for (int i = 0; i < rows; i++) {
cout << "[";
for (int j = 0; j < cols; j++) {
cout << matrix[i][j];
if (j != cols - 1) {
cout << ",";
}
}
cout << "]";
if (i != rows - 1) {
cout << ",";
}
}
cout << "]";
}
};