-
Notifications
You must be signed in to change notification settings - Fork 20
/
SetMatrixZeroes.js
81 lines (70 loc) · 1.87 KB
/
SetMatrixZeroes.js
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
73
74
75
76
77
78
79
80
81
/**
* Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
*
* Accepted.
*/
/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
let setZeroes = function (matrix) {
if (matrix.length === 0 || matrix[0].length === 0) {
return;
}
let row = new Set();
let column = new Set();
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] === 0) {
row.add(i);
column.add(j);
}
}
}
row.forEach(function (i) {
for (let k = 0; k < matrix[0].length; k++) {
matrix[i][k] = 0;
}
});
column.forEach(function (i) {
for (let k = 0; k < matrix.length; k++) {
matrix[k][i] = 0;
}
});
// console.log(matrix);
};
let array0 = [];
setZeroes(array0);
if (array0.toString() === [].toString()) {
console.log("pass")
} else {
console.error("failed")
}
let array1 = [[0, 1]];
setZeroes(array1);
if (array1.toString() === [[0, 0]].toString()) {
console.log("pass")
} else {
console.error("failed")
}
let array2 = [[1, 0, 2], [3, 4, 5], [6, 7, 8]];
setZeroes(array2);
if (array2.toString() === [[0, 0, 0], [3, 0, 5], [6, 0, 8]].toString()) {
console.log("pass")
} else {
console.error("failed")
}
let array3 = [[0, 1, 1], [2, 0, 2], [3, 3, 0]];
setZeroes(array3);
if (array3.toString() === [[0, 0, 0], [0, 0, 0], [0, 0, 0]].toString()) {
console.log("pass")
} else {
console.error("failed")
}
let array4 = [[0, 0, 0, 5], [4, 3, 1, 4], [0, 1, 1, 4], [1, 2, 1, 3], [0, 0, 1, 1]];
setZeroes(array4);
if (array4.toString() === [[0, 0, 0, 0], [0, 0, 0, 4], [0, 0, 0, 0], [0, 0, 0, 3], [0, 0, 0, 0]].toString()) {
console.log("pass")
} else {
console.error("failed")
}