-
Notifications
You must be signed in to change notification settings - Fork 0
/
setZeroes.java
50 lines (45 loc) · 1.3 KB
/
setZeroes.java
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
function setZeroes(matrix) {
const rows = matrix.length;
const cols = matrix[0].length;
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (matrix[i][j] === 1) {
let ind = i - 1;
while (ind >= 0) {
if (matrix[ind][j] !== 1) {
matrix[ind][j] = -1;
}
ind--;
}
ind = i + 1;
while (ind < rows) {
if (matrix[ind][j] !== 1) {
matrix[ind][j] = -1;
}
ind++;
}
ind = j - 1;
while (ind >= 0) {
if (matrix[i][ind] !== 1) {
matrix[i][ind] = -1;
}
ind--;
}
ind = j + 1;
while (ind < cols) {
if (matrix[i][ind] !== 1) {
matrix[i][ind] = -1;
}
ind++;
}
}
}
}
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (matrix[i][j] < 0) {
matrix[i][j] = 1;
}
}
}
}