File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 1+ package leetcode_study
2+
3+ /*
4+ * board에서 0이 발견 되었을 때, 기준 row, column을 모두 0으로 변경하는 문제
5+ * Set 자료구조를 사용해 중복을 없애고 순회하여 문제 해결
6+ * 시간 복잡도: O(n^2) (정확히는 O(row * col))
7+ * -> board의 row, col을 순회해 0을 찾는 과정
8+ * -> set으로 중복을 제거한 row을 순회해 board의 값을 0으로 변경하는 과정
9+ * -> set으로 중복을 제거한 column을 순회해 board의 값을 0으로 변경하는 과정
10+ * 공간 복잡도: O(1)
11+ * -> 중복을 제거한 rowSet, columnSet을 저장하는 공간
12+ * */
13+ fun setZeroes (matrix : Array <IntArray >): Unit {
14+ val rowSet = mutableSetOf<Int >()
15+ val colSet = mutableSetOf<Int >()
16+
17+ val rowSize = matrix.size
18+ val colSize = matrix[0 ].size
19+
20+ // find row col
21+ for (row in matrix.indices) {
22+ for (col in matrix[0 ].indices) {
23+ if (matrix[row][col] == 0 ) {
24+ rowSet.add(row)
25+ colSet.add(col)
26+ }
27+ }
28+ }
29+
30+ if (rowSet.size == 0 && colSet.size == 0 ) return
31+
32+ for (row in rowSet) {
33+ for (index in IntRange (0 , colSize - 1 )) {
34+ matrix[row][index] = 0
35+ }
36+ }
37+
38+ for (col in colSet) {
39+ for (index in IntRange (0 , rowSize - 1 )) {
40+ matrix[index][col] = 0
41+ }
42+ }
43+ }
You can’t perform that action at this time.
0 commit comments