Skip to content

Commit 27e2dd7

Browse files
committed
feat: 문제풀이 추가
1 parent 06d61ed commit 27e2dd7

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

set-matrix-zeroes/hwamini.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// 시간복잡도: O((n * m) * (n + m))
2+
// 공간복잡도: O(n * m)
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+
const zeroIndexes = []
10+
11+
for (let row = 0; row < matrix.length; row++) {
12+
for (let col = 0; col < matrix[row].length; col++) {
13+
if (matrix[row][col] === 0) {
14+
zeroIndexes.push([row,col])
15+
}
16+
}
17+
}
18+
19+
while (zeroIndexes.length) {
20+
const [row, col] = zeroIndexes.pop()
21+
22+
for (let i = 0; i < matrix[0].length; i++) {
23+
matrix[row][i] = 0
24+
}
25+
26+
for (let j = 0; j < matrix.length; j++) {
27+
matrix[j][col] = 0
28+
}
29+
30+
}
31+
32+
return matrix
33+
};
34+
35+
console.log(setZeroes([
36+
[1,1,1],
37+
[1,0,1],
38+
[1,1,1]]))
39+
console.log(setZeroes([[0,1,2,0],[3,4,5,2],[1,3,1,5]]))

0 commit comments

Comments
 (0)