Skip to content

Commit 1ff8599

Browse files
committed
set-matrix-zeroes solved
1 parent 997d729 commit 1ff8599

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

set-matrix-zeroes/hsskey.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const setZeroes = (matrix) => {
2+
const ROWS = matrix.length;
3+
const COLS = matrix[0].length;
4+
let rowZero = false;
5+
6+
// 1. 어떤 행과 열이 0이 되어야 하는지 기록
7+
for (let r = 0; r < ROWS; r++) {
8+
for (let c = 0; c < COLS; c++) {
9+
if (matrix[r][c] === 0) {
10+
matrix[0][c] = 0;
11+
if (r > 0) {
12+
matrix[r][0] = 0;
13+
} else {
14+
rowZero = true;
15+
}
16+
}
17+
}
18+
}
19+
20+
// 2. 첫 번째 행과 첫 번째 열을 제외한 나머지 처리
21+
for (let r = 1; r < ROWS; r++) {
22+
for (let c = 1; c < COLS; c++) {
23+
if (matrix[0][c] === 0 || matrix[r][0] === 0) {
24+
matrix[r][c] = 0;
25+
}
26+
}
27+
}
28+
29+
// 3. 첫 번째 열 처리
30+
if (matrix[0][0] === 0) {
31+
for (let r = 0; r < ROWS; r++) {
32+
matrix[r][0] = 0;
33+
}
34+
}
35+
36+
// 4. 첫 번째 행 처리
37+
if (rowZero) {
38+
for (let c = 0; c < COLS; c++) {
39+
matrix[0][c] = 0;
40+
}
41+
}
42+
};

0 commit comments

Comments
 (0)