-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path73.setMatrixZero.cpp
More file actions
35 lines (30 loc) · 924 Bytes
/
73.setMatrixZero.cpp
File metadata and controls
35 lines (30 loc) · 924 Bytes
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
/*Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's.
You must do it in place.*/
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
unordered_set<int> col;
unordered_set<int> row;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(matrix[i][j] == 0){
if(!row.count(i)){
row.insert(i);
}
if(!col.count(j)){
col.insert(j);
}
}
}
}
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(row.count(i) || col.count(j)){
matrix[i][j] = 0;
}
}
}
}
};