-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset-matrix-zeros.cpp
45 lines (37 loc) · 1016 Bytes
/
set-matrix-zeros.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
// No main function
#include <bits/stdc++.h>
using namespace std;
void setZeros(vector<vector<int>> &matrix) {
int n = matrix.size(), m = matrix[0].size();
int tmp = 1; // Indicator for 1st column - whether it should be 0 or not.
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matrix[i][j] == 0) {
matrix[i][0] = 0;
if (j == 0)
tmp = 0;
else
matrix[0][j] = 0;
}
}
}
for (int i = n-1; i >= 1; i--) {
for (int j = m-1; j >= 1; j--) {
if (matrix[i][j] != 0) {
if (matrix[i][0] == 0 or matrix[0][j] == 0) {
matrix[i][j] = 0;
}
}
}
}
if (matrix[0][0] == 0) {
for (int j = 0; j < m; j++) {
matrix[0][j] = 0;
}
}
if (tmp == 0) {
for (int i = 0; i < n; i++) {
matrix[i][0] = 0;
}
}
}