Skip to content

Commit 1a86a2a

Browse files
author
eunhwa99
committed
set matrix zeroes
1 parent f4443eb commit 1a86a2a

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

โ€Žset-matrix-zeroes/eunhwa99.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
// ์‹œ๊ฐ„ ๋ณต์žก๋„: O(row x col)
5+
// ๊ณต๊ฐ„ ๋ณต์žก๋„: O(row x col)
6+
class Solution {
7+
8+
public void setZeroes(int[][] matrix) {
9+
int row = matrix.length;
10+
int col = matrix[0].length;
11+
12+
// ํ–‰๊ณผ ์—ด์— 0์ด ์žˆ๋Š”์ง€ ์ฒดํฌํ•  ๋ฐฐ์—ด
13+
Set<Integer> rowZero = new HashSet<>();
14+
Set<Integer> colZero = new HashSet<>();
15+
16+
for (int i = 0; i < row; i++) {
17+
for (int j = 0; j < col; j++) {
18+
if (matrix[i][j] == 0) {
19+
rowZero.add(i);
20+
colZero.add(j);
21+
}
22+
}
23+
}
24+
25+
// ํ–‰์„ 0์œผ๋กœ ์„ค์ •
26+
for (int r : rowZero) {
27+
for (int c = 0; c < col; c++) {
28+
matrix[r][c] = 0;
29+
}
30+
}
31+
32+
// ์—ด์„ 0์œผ๋กœ ์„ค์ •
33+
for (int c : colZero) {
34+
for (int r = 0; r < row; r++) {
35+
matrix[r][c] = 0;
36+
}
37+
}
38+
}
39+
}
40+

0 commit comments

Comments
ย (0)