-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathSetMatrixZeroes.kt
More file actions
executable file
路38 lines (32 loc) 路 880 Bytes
/
Copy pathSetMatrixZeroes.kt
File metadata and controls
executable file
路38 lines (32 loc) 路 880 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
*
* Accepted.
*/
class SetMatrixZeroes {
fun setZeroes(matrix: Array<IntArray>) {
if (matrix.isEmpty() || matrix[0].isEmpty()) {
return
}
val row = HashSet<Int>(matrix.size)
val column = HashSet<Int>(matrix[0].size)
matrix.indices.forEach { i ->
(0 until matrix[0].size).forEach {
if (matrix[i][it] == 0) {
row.add(i)
column.add(it)
}
}
}
row.forEach { i ->
(0 until matrix[0].size).forEach {
matrix[i][it] = 0
}
}
column.forEach { i ->
(0 until matrix.size).forEach {
matrix[it][i] = 0
}
}
}
}