-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetZeroes.js
More file actions
36 lines (33 loc) · 820 Bytes
/
setZeroes.js
File metadata and controls
36 lines (33 loc) · 820 Bytes
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
/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var setZeroes = function(matrix) {
if (matrix.length === 0) {
return matrix;
}
const rows = new Set();
const cols = new Set();
for (let i = 0, len = matrix.length; i < len; i++) {
for (let j = 0, l = matrix[i].length; j < l; j++) {
if (matrix[i][j] === 0) {
rows.add(i);
cols.add(j);
}
}
}
for (let i = 0, len = matrix.length; i < len; i++) {
if (rows.has(i)) {
for (let j = 0, l = matrix[i].length; j < l; j++) {
matrix[i][j] = 0;
}
}
}
for (let j = 0, len = matrix[0].length; j < len; j++) {
if (cols.has(j)) {
for (let i = 0, l = matrix.length; i < l; i++) {
matrix[i][j] = 0;
}
}
}
};