Skip to content

Commit a883d95

Browse files
committed
solve problem
1 parent 47b8b32 commit a883d95

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

combination-sum/delight010.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
3+
var result: [[Int]] = []
4+
var current: [Int] = []
5+
findCombination(0, target, candidates, &current, &result)
6+
return result
7+
}
8+
9+
func findCombination(_ index: Int, _ target: Int, _ candidates: [Int], _ current: inout [Int], _ result: inout [[Int]]) {
10+
if target == 0 {
11+
result.append(current)
12+
return
13+
}
14+
15+
for i in index..<candidates.count {
16+
if candidates[i] <= target {
17+
current.append(candidates[i])
18+
findCombination(i, target - candidates[i], candidates, &current, &result)
19+
current.removeLast()
20+
}
21+
}
22+
}
23+
}
24+

0 commit comments

Comments
 (0)