-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode_73.cpp
More file actions
61 lines (51 loc) · 1.46 KB
/
Copy pathLeetCode_73.cpp
File metadata and controls
61 lines (51 loc) · 1.46 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
// class Solution {
// public:
// void setZeroes(vector<vector<int>>& matrix) {
// vector<pair<int,int>>arr;
// int num_row = matrix.size();
// int num_col = matrix[0].size();
// for(int i = 0; i<num_row;i++){
// for(int j = 0; j<num_col;j++){
// if(matrix[i][j]==0){
// arr.push_back({i,j});
// }
// }
// }
// for(int k = 0;k<arr.size();k++){
// int row = arr[k].first;
// int col = arr[k].second;
// for(int i=0;i<num_row;i++){
// matrix[i][col]=0;
// }
// for(int j=0;j<num_col;j++){
// matrix[row][j]=0;
// }
// }
// }
// };
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int rows = matrix.size();
int cols = matrix[0].size();
vector<int> row(rows, 0);
vector<int> col(cols, 0);
// mark rows and cols
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
if(matrix[i][j] == 0) {
row[i] = 1;
col[j] = 1;
}
}
}
// set zeroes
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
if(row[i] || col[j]) {
matrix[i][j] = 0;
}
}
}
}
};