Skip to content

Commit c1cb53a

Browse files
committed
Kotlin: 90. Subsets II
1 parent fd08e46 commit c1cb53a

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Diff for: kotlin/90-Subsets-II.kt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
fun subsetsWithDup(nums: IntArray): List<List<Int>> {
3+
val res = mutableListOf<List<Int>>()
4+
Arrays.sort(nums)
5+
subsets(nums, 0, mutableListOf<Int>(), mutableSetOf<List<Int>>(), res)
6+
return res
7+
}
8+
9+
fun subsets(nums: IntArray, idx: Int, list: MutableList<Int>, set: MutableSet<List<Int>>, res: MutableList<List<Int>>) {
10+
val copy = ArrayList(list)
11+
12+
if (set.add(copy))
13+
res.add(copy)
14+
15+
for (i in idx..nums.size-1) {
16+
list.add(nums[i])
17+
subsets(nums, i+1, list, set, res)
18+
list.removeAt(list.size-1)
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)