-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombination-sum.go
38 lines (33 loc) · 889 Bytes
/
combination-sum.go
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
package leetcode_solutions_golang
import "container/list"
//https://leetcode.com/problems/combination-sum/
func combinationSum(candidates []int, target int) [][]int {
type State struct {
result []int
total int
pos int
}
q := list.New()
q.PushBack(State{make([]int, 0), 0, 0})
combinations := make([][]int, 0)
for q.Len() > 0 {
el := q.Front()
curState := el.Value.(State)
q.Remove(el)
if curState.total == target {
combinations = append(combinations, curState.result)
continue
}
for i := curState.pos; i < len(candidates); i++ {
if curState.total+candidates[i] <= target {
resultCopy := make([]int, 0)
for _, j := range curState.result {
resultCopy = append(resultCopy, j)
}
resultCopy = append(resultCopy, candidates[i])
q.PushBack(State{resultCopy, curState.total + candidates[i], i})
}
}
}
return combinations
}