Skip to content

Commit 5b55cc9

Browse files
committed
[leet] set matrix zero (mid)
1 parent 117dc26 commit 5b55cc9

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @return {void} Do not return anything, modify matrix in-place instead.
4+
*/
5+
var setZeroes = function (matrix) {
6+
const dir = [
7+
[1, 0],
8+
[-1, 0],
9+
[0, 1],
10+
[0, -1],
11+
];
12+
13+
const q = [];
14+
for (let i = 0; i < matrix.length; i++) {
15+
for (let j = 0; j < matrix[0].length; j++) {
16+
if (matrix[i][j] === 0) {
17+
q.push([i, j]);
18+
}
19+
}
20+
}
21+
while (q.length) {
22+
const [curY, curX] = q.shift();
23+
for (let i = 0; i < matrix.length; i++) {
24+
matrix[i][curX] = 0;
25+
}
26+
for (let i = 0; i < matrix[0].length; i++) {
27+
matrix[curY][i] = 0;
28+
}
29+
}
30+
};

0 commit comments

Comments
 (0)