Skip to content

Commit 7272cda

Browse files
committed
feat: solve combination sum
1 parent 940096f commit 7272cda

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

combination-sum/Real-Reason.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package leetcode_study
2+
3+
fun combinationSum(candidates: IntArray, target: Int): List<List<Int>> {
4+
val result = mutableListOf<List<Int>>()
5+
val nums = ArrayDeque<Int>()
6+
dfs(candidates, target, 0, 0, nums, result)
7+
8+
return result
9+
}
10+
11+
private fun dfs(candidates: IntArray, target: Int, startIdx: Int, total: Int, nums: ArrayDeque<Int>, result: MutableList<List<Int>>) {
12+
if (target < total) return
13+
if (target == total) {
14+
result.add(ArrayList(nums))
15+
return
16+
}
17+
for (i in startIdx..< candidates.size) {
18+
val num = candidates[i]
19+
nums.add(num)
20+
dfs(candidates, target, i, total + num, nums, result)
21+
nums.removeLast()
22+
}
23+
}

product-of-array-except-self/Real-Reason.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ fun productExceptSelf(nums: IntArray): IntArray {
2626
}
2727

2828
return result.toIntArray()
29-
}
29+
}

two-sum/Real-Reason.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ class `Real-Reason` {
1313
}
1414
throw RuntimeException("There is no solution")
1515
}
16-
}
16+
}

0 commit comments

Comments
 (0)