-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path200117-1.cpp
72 lines (69 loc) · 1.1 KB
/
200117-1.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
72
// https://leetcode-cn.com/problems/set-matrix-zeroes/
#include <cstdio>
#include <vector>
#include <unordered_set>
using namespace std;
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int m = matrix.size();
if (m <= 0) return;
int n = matrix[0].size();
if (n <= 0) return;
unordered_set<int> r, c;
for (int y = 0; y < m; ++y) {
for (int x = 0; x < n; ++x) {
if (matrix[y][x] == 0) {
r.insert(y);
c.insert(x);
}
}
}
for (auto y : r) {
for (int x = 0; x < n; ++x) {
matrix[y][x] = 0;
}
}
for (auto x : c) {
for (int y = 0; y < m; ++y) {
matrix[y][x] = 0;
}
}
}
};
void print(const vector<vector<int>>& a)
{
printf("[\n");
for (const auto& r : a) {
printf(" [");
int i = 0;
for (auto e : r) {
printf("%s%d", (i++?",":""), e);
}
printf("]\n");
}
printf("]\n");
}
int main()
{
Solution s;
{
vector<vector<int>> a = {
{1,1,1},
{1,0,1},
{1,1,1}
};
s.setZeroes(a);
print(a);
}
{
vector<vector<int>> a = {
{0,1,2,0},
{3,4,5,2},
{1,3,1,5}
};
s.setZeroes(a);
print(a);
}
return 0;
}