Skip to content

Commit 719eec2

Browse files
authored
Create 73-set-matrix-zeroes.js
1 parent 18ed1ba commit 719eec2

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

73-set-matrix-zeroes.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @return {void} Do not return anything, modify matrix in-place instead.
4+
*/
5+
const setZeroes = function(matrix) {
6+
const rows = []
7+
const cols = []
8+
const rowNum = matrix.length
9+
const colNum = matrix[0].length
10+
for(let i = 0; i < rowNum; i++) {
11+
for(let j = 0; j < colNum; j++) {
12+
if(matrix[i][j] === 0) {
13+
rows.push(i)
14+
cols.push(j)
15+
}
16+
}
17+
}
18+
const mrows = rows.filter((el, idx, arr) => arr.indexOf(el) === idx)
19+
const mcols = cols.filter((el, idx, arr) => arr.indexOf(el) === idx)
20+
for(let i = 0; i < mrows.length; i++) {
21+
matrix[mrows[i]] = new Array(colNum).fill(0)
22+
}
23+
for(let j = 0; j < mcols.length; j++) {
24+
for(let k = 0; k < rowNum; k++) {
25+
matrix[k][mcols[j]] = 0
26+
}
27+
}
28+
return matrix
29+
};

0 commit comments

Comments
 (0)