-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc73.cpp
More file actions
70 lines (57 loc) · 1.41 KB
/
lc73.cpp
File metadata and controls
70 lines (57 loc) · 1.41 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <vector>
#include <cstdio>
using namespace std;
class Solution {
int nr, nc;
public:
void setRowZero(vector<vector<int>>& matrix, int row) {
for(int c = 0; c < this->nc; c++) {
matrix[row][c] = 0;
}
}
void setColumnZero(vector<vector<int>>& matrix, int column) {
for(int r = 0; r < this->nr; r++) {
matrix[r][column] = 0;
}
}
void setZeroes(vector<vector<int>>& matrix) {
this->nr = matrix.size();
this->nc = matrix[0].size();
vector<int> rows(nr, -1);
vector<int> cols(nc, -1);
for(int i=0; i < this->nr; i++) {
for(int j=0; j < this->nc; j++) {
if (matrix[i][j] == 0) {
rows[i] = 0;
cols[j] = 0;
}
}
}
for(int i=0; i < this->nr; i++) {
if (rows[i] == 0) {
setRowZero(matrix,i);
}
}
for(int j=0; j < this->nc; j++) {
if (cols[j] == 0) {
setColumnZero(matrix,j);
}
}
}
};
int main() {
Solution* s = new Solution();
vector<vector<int>> matrix = {
{1,1,1},
{1,0,1},
{1,1,1}
};
s->setZeroes(matrix);
for(auto row : matrix) {
for(auto col : row) {
printf("%d ", col);
}
printf("\n");
}
return 0;
}