We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e113d55 commit 5e112c3Copy full SHA for 5e112c3
combination-sum/WhiteHyun.swift
@@ -7,7 +7,30 @@
7
//
8
9
class Solution {
10
- func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
11
-
+ func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
+ let sortedCandidates = candidates.sorted()
12
+ var answer: [[Int]] = []
13
+
14
+ func backtrack(_ sum: Int, _ start: Int, _ current: [Int]) {
15
+ if sum == target {
16
+ answer.append(current)
17
+ return
18
+ }
19
20
+ if sum > target {
21
22
23
24
+ for i in start ..< sortedCandidates.count {
25
+ let newSum = sum + sortedCandidates[i]
26
+ if newSum > target {
27
+ break
28
29
+ backtrack(newSum, i, current + [sortedCandidates[i]])
30
31
}
32
33
+ backtrack(0, 0, [])
34
+ return answer
35
36
0 commit comments