-
Notifications
You must be signed in to change notification settings - Fork 0
/
8-set-matrix-zeroes.cpp
69 lines (62 loc) · 1.61 KB
/
8-set-matrix-zeroes.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
class Solution
{
public:
#define f(i, a, b) for (int i = a; i <= b; i++)
#define vi vector<int>
void setZeroes(vector<vector<int>> &mat)
{
int m = mat.size(), n = mat[0].size();
int f1 = 0, f2 = 0;
// first checking first row and first column
// if first row contains a '0', f1 = 1
// if first column contains a '0', f2 = 1
f(i, 0, n - 1)
{
if (mat[0][i] == 0)
{
f1 = 1;
break;
}
}
f(i, 0, m - 1)
{
if (mat[i][0] == 0)
{
f2 = 1;
break;
}
}
// now the turn of other rows and columns
f(i, 1, m - 1)
{
f(j, 1, n - 1)
{
// if any element is 0:
// make the first element of its row and first element of its
// column 0
if (mat[i][j] == 0)
mat[i][0] = mat[0][j] = 0;
}
}
// if either the first row element or the first column element is 0,
// make that element 0
f(i, 1, m - 1)
{
f(j, 1, n - 1)
{
if (mat[i][0] == 0 || mat[0][j] == 0)
mat[i][j] = 0;
}
}
// if first row contains a 0, make the whole row zero
if (f1)
{
f(i, 0, n - 1) mat[0][i] = 0;
}
// if first column contains a 0, make the whole column zero
if (f2)
{
f(i, 0, m - 1) mat[i][0] = 0;
}
}
};