-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathP40_2.kt
More file actions
28 lines (24 loc) · 823 Bytes
/
P40_2.kt
File metadata and controls
28 lines (24 loc) · 823 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
package ch12
import java.util.*
class P40_2 {
fun subsets(nums: IntArray): List<List<Int>> {
// 결과 저장 리스트 선언
val results: MutableList<List<Int>> = mutableListOf()
fun dfs(index: Int, path: Deque<Int>) {
// 모든 탐색 경로를 매번 결과에 추가
results.add(ArrayList(path))
// 자기 자신보다 큰 숫자를 DFS 진행
for (i in index until nums.size) {
// path에 현재 엘리먼트 추가
path.add(nums[i])
// 재귀 DFS
dfs(i + 1, path)
// 돌아온 이후에는 현재 엘리먼트 삭제
path.removeLast()
}
}
// DFS 시작
dfs(0, ArrayDeque())
return results
}
}