-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMed_78_SubSet.kt
More file actions
45 lines (39 loc) · 1019 Bytes
/
Copy pathMed_78_SubSet.kt
File metadata and controls
45 lines (39 loc) · 1019 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
39
40
41
42
43
44
45
package com.boycoder.problems.backtrack
/**
* @Author: zhutao
* @datetime: 2021/6/22
* @desc:
*
* Ideal
* For the sub set of a set. When we trying to find the combination, all the node of this tree(state of path), are the no-duplicate subset
*
* eg: n = 3
*
* [1,2,3]
* 1/ 2| \3
* [2,3] [3] []
* 2/ \3 |3
* [3] [] []
* 3/
* []
*
*/
object Med_78_SubSet {
private val list: MutableList<List<Int>> = mutableListOf()
private val path: MutableList<Int> = mutableListOf()
fun subsets(nums: IntArray): List<List<Int>> {
sub(nums, 0)
return list
}
private fun sub(nums: IntArray, start: Int) {
list.add(path.toList())
if (start >= nums.size) {
return
}
for (i in start until nums.size) {
path.add(nums[i])
sub(nums, i + 1)
path.removeAt(path.size - 1)
}
}
}