Skip to content

Commit c639f7c

Browse files
committed
add: set matrix zeroes
1 parent 8ef7d4b commit c639f7c

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

number-of-islands/HerrineKim.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// 시간 복잡도: O(m * n)
2+
// 공간 복잡도: O(m * n)
3+
14
/**
25
* @param {character[][]} grid
36
* @return {number}

set-matrix-zeroes/HerrineKim.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// 시간 복잡도: O(m * n)
2+
// 공간 복잡도: O(m + n)
3+
4+
/**
5+
* @param {number[][]} matrix
6+
* @return {void} Do not return anything, modify matrix in-place instead.
7+
*/
8+
var setZeroes = function (matrix) {
9+
if (!matrix || matrix.length === 0) return [];
10+
11+
const rows = matrix.length;
12+
const cols = matrix[0].length;
13+
14+
const zeroRows = new Array(rows).fill(false);
15+
const zeroCols = new Array(cols).fill(false);
16+
17+
for (let row = 0; row < rows; row++) {
18+
for (let col = 0; col < cols; col++) {
19+
if (matrix[row][col] === 0) {
20+
zeroRows[row] = true;
21+
zeroCols[col] = true;
22+
}
23+
}
24+
}
25+
26+
for (let row = 0; row < rows; row++) {
27+
for (let col = 0; col < cols; col++) {
28+
if (zeroRows[row] || zeroCols[col]) {
29+
matrix[row][col] = 0;
30+
}
31+
}
32+
}
33+
34+
return matrix;
35+
};

0 commit comments

Comments
 (0)