Skip to content

Commit 3313e13

Browse files
committed
Add set matrix zeroes solution
1 parent 06aa8e3 commit 3313e13

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

set-matrix-zeroes/hyunjung-choi.kt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* 시간복잡도: O(m × n)
3+
* 공간복잡도: O(1)
4+
*/
5+
6+
class Solution {
7+
fun setZeroes(matrix: Array<IntArray>): Unit {
8+
val m = matrix.size
9+
val n = matrix[0].size
10+
11+
var firstRowHasZero = false
12+
var firstColumnHasZero = false
13+
14+
for (j in 0 until n) {
15+
if (matrix[0][j] == 0) {
16+
firstRowHasZero = true
17+
break
18+
}
19+
}
20+
21+
for (i in 0 until m) {
22+
if (matrix[i][0] == 0) {
23+
firstColumnHasZero = true
24+
break
25+
}
26+
}
27+
28+
for (i in 1 until m) {
29+
for (j in 1 until n) {
30+
if (matrix[i][j] == 0) {
31+
matrix[i][0] = 0
32+
matrix[0][j] = 0
33+
}
34+
}
35+
}
36+
37+
for (i in 1 until m) {
38+
for (j in 1 until n) {
39+
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
40+
matrix[i][j] = 0
41+
}
42+
}
43+
}
44+
45+
for (j in 0 until n) {
46+
if (firstRowHasZero) {
47+
matrix[0][j] = 0
48+
}
49+
}
50+
51+
for (i in 0 until m) {
52+
if (firstColumnHasZero) {
53+
matrix[i][0] = 0
54+
}
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)