-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode-73-Set_Matrix_Zeroes-V1.cpp
71 lines (59 loc) · 1.58 KB
/
leetcode-73-Set_Matrix_Zeroes-V1.cpp
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
71
#include <iostream>
#include <cstdlib>
#include <vector>
#include <unordered_map>
class Solution {
public:
void setZeroes(std::vector<std::vector<int>>& matrix) {
std::unordered_map<size_t, size_t> row_v;
std::unordered_map<size_t, size_t> col_v;
for(size_t r = 0; r<matrix.size(); ++r)
{
for(size_t c = 0; c<matrix[r].size(); ++c)
{
if(!matrix[r][c])
{
++row_v[r];
++col_v[c];
}
}
}
for(const auto& x:row_v)
{
size_t r = x.first;
for(size_t c = 0; c<matrix[r].size(); ++c)
{
matrix[r][c] = 0;
}
}
for(const auto& x:col_v)
{
size_t c = x.first;
for(size_t r = 0; r<matrix.size(); ++r)
{
matrix[r][c] = 0;
}
}
}
};
int main(){
std::vector<std::vector<int>> matrix = {{1,1,1},{1,0,1},{1,1,1}};
// std::vector<std::vector<int>> matrix = {{0,1,2,0},{3,4,5,2},{1,3,1,5}};
Solution().setZeroes(matrix);
std::cout << "[" ;
for(size_t i = 0; i<matrix.size(); ++i)
{
std::cout << "[";
for(size_t j = 0; j<matrix[i].size(); ++j)
{
std::cout << matrix[i][j];
if(j!=matrix[i].size()-1)
std::cout << ",";
}
std::cout << "]";
if(i!=matrix.size()-1)
std::cout << ",";
}
std::cout << "]" << std::endl;
return EXIT_SUCCESS;
}