Skip to content

Commit 17b3360

Browse files
committed
[Week7](gmlwls96) Set Matrix Zeroes
1 parent 2cd028d commit 17b3360

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

set-matrix-zeroes/gmlwls96.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
// 시간 : O(mn) 공간 : O(m+n)
3+
// 변경해야되는 position 찾아 yList, xList에 각각 y,x값을 담되 set을 이용하여 중복을 제거한다.
4+
// yList, xList를 조회하며 matrix를 변경해준다.
5+
fun setZeroes(matrix: Array<IntArray>): Unit {
6+
val yList = mutableSetOf<Int>()
7+
val xList = mutableSetOf<Int>()
8+
for (y in 0 until matrix.size) {
9+
for (x in 0 until matrix[y].size) {
10+
if (matrix[y][x] == 0) {
11+
yList.add(y)
12+
xList.add(x)
13+
}
14+
}
15+
}
16+
yList.forEach { y ->
17+
for (x in 0 until matrix[y].size) {
18+
matrix[y][x] = 0
19+
}
20+
}
21+
xList.forEach { x ->
22+
for (y in 0 until matrix.size) {
23+
matrix[y][x] = 0
24+
}
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)