Skip to content

Commit 4847a02

Browse files
committed
feat: 73. Set Matrix Zeroes
1 parent e7f8a28 commit 4847a02

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

set-matrix-zeroes/gwbaik9717.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// m: height of matrix, n: width of matrix
2+
// Time complexity: O(m*n)
3+
// Space complexity: O(m+n)
4+
5+
/**
6+
* @param {number[][]} matrix
7+
* @return {void} Do not return anything, modify matrix in-place instead.
8+
*/
9+
var setZeroes = function (matrix) {
10+
const h = matrix.length;
11+
const w = matrix[0].length;
12+
13+
const setY = new Set();
14+
const setX = new Set();
15+
16+
for (let i = 0; i < h; i++) {
17+
for (let j = 0; j < w; j++) {
18+
if (matrix[i][j] === 0) {
19+
setY.add(i);
20+
setX.add(j);
21+
}
22+
}
23+
}
24+
25+
for (const y of setY) {
26+
for (let x = 0; x < w; x++) {
27+
matrix[y][x] = 0;
28+
}
29+
}
30+
31+
for (const x of setX) {
32+
for (let y = 0; y < h; y++) {
33+
matrix[y][x] = 0;
34+
}
35+
}
36+
};

0 commit comments

Comments
 (0)