-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0039_combination_sum.go
More file actions
41 lines (34 loc) · 1.12 KB
/
s0039_combination_sum.go
File metadata and controls
41 lines (34 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
https://leetcode.com/problems/combination-sum/
Given an array of distinct integers candidates and a target integer target,
return a list of all unique
combinations of candidates where the chosen numbers sum to target. You may
return the combinations in any order.
The same number may be chosen from candidates an unlimited number of times. Two
combinations are unique if the
frequency of at least one of the chosen numbers is different.
It is guaranteed that the number of unique combinations that sum up to target
is less than 150 combinations for
the given input.
*/
package solutions
func backtrack39(remain int, curr []int, fst int, acc *[][]int, candidates []int) {
if remain == 0 {
cp := make([]int, len(curr))
copy(cp, curr)
*acc = append(*acc, cp)
return
} else if remain < 0 {
return
}
for i := fst; i < len(candidates); i++ {
curr = append(curr, candidates[i])
backtrack39(remain-candidates[i], curr, i, acc, candidates)
curr = curr[:len(curr)-1]
}
}
func combinationSum(candidates []int, target int) [][]int {
var res [][]int
backtrack39(target, []int{}, 0, &res, candidates)
return res
}