Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions kotlin/40-Combination-Sum.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
fun combinationSum2(candidates: IntArray, target: Int): List<List<Int>> {
val res = mutableListOf<List<Int>>()
candidates.sort()
dfs(candidates, target, 0, mutableListOf<Int>(), res)
return res
}

fun dfs(nums: IntArray, target: Int, idx: Int, list: MutableList<Int>, res: MutableList<List<Int>>) {

if (target == 0) {
res.add(ArrayList(list))
return
}

if (idx >= nums.size || target < 0)
return

for (i in idx..nums.size-1) {
if (i == idx || nums[i] != nums[i-1]) {
list.add(nums[i])
dfs(nums, target - nums[i], i+1, list, res)
list.removeAt(list.size-1)
}
}
}
}