12345678910111213141516171819202122232425262728293031323334 |
- package main
- import (
- "sort"
- )
- func combinationSum2Iter(candidates []int, target int, last []int, beg int, res *[][]int) {
- if target == 0 {
- *res = append(*res, last)
- return
- }
- for i := beg; i < len(candidates) && candidates[i] <= target; i++ {
-
- if i > beg && candidates[i] == candidates[i-1] {
- continue
- }
- solution := make([]int, len(last))
- copy(solution, last)
- solution = append(solution, candidates[i])
- combinationSum2Iter(candidates, target-candidates[i], solution, i+1, res)
- }
- }
- func combinationSum2(candidates []int, target int) [][]int {
- res := make([][]int, 0)
- sort.Ints(candidates)
- combinationSum2Iter(candidates, target, []int{}, 0, &res)
- return res
- }
|