-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(leetcode/medium/39-combination-sum.go)
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
coding-challange/leetcode/medium/39-combination-sum/39-combination-sum.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
39-combination-sum | ||
leetcode/medium/39. Combination Sum | ||
URL: https://leetcode.com/problems/combination-sum/ | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
) | ||
|
||
func backtrack(candidates []int, target int, path []int, result *[][]int, resultSet *map[string]bool) { | ||
if target < 0 { | ||
return | ||
} | ||
|
||
if target == 0 { | ||
sort.Ints(path) | ||
key := fmt.Sprintf("%v", path) | ||
|
||
if _, ok := (*resultSet)[key]; !ok { | ||
*result = append(*result, path) | ||
(*resultSet)[key] = true | ||
} | ||
return | ||
} | ||
|
||
for i := len(candidates) - 1; i >= 0; i-- { | ||
new_path := append([]int{candidates[i]}, path...) | ||
backtrack(candidates, target-candidates[i], new_path, result, resultSet) | ||
} | ||
} | ||
|
||
func combinationSum(candidates []int, target int) [][]int { | ||
sort.Ints(candidates) | ||
|
||
var result [][]int | ||
var path []int | ||
var resultSet = make(map[string]bool) | ||
|
||
backtrack(candidates, target, path, &result, &resultSet) | ||
|
||
return result | ||
} | ||
|
||
func main() { | ||
candidates := []int{2, 3, 6, 7} | ||
target := 7 | ||
//fmt.Println(combinationSum(candidates, target)) | ||
//Output: [[2, 2, 3], [7]] | ||
|
||
candidates = []int{2, 3, 5} | ||
target = 8 | ||
//fmt.Println(combinationSum(candidates, target)) | ||
//Output: [[2, 2, 2, 2], [2, 3, 3], [3, 5]] | ||
|
||
candidates = []int{2} | ||
target = 1 | ||
//fmt.Println(combinationSum(candidates, target)) | ||
//Output: [] | ||
|
||
candidates = []int{7, 3, 2} | ||
target = 18 | ||
fmt.Println(combinationSum(candidates, target)) | ||
//[[7,7,2,2],[7,3,3,3,2],[7,3,2,2,2,2],[3,3,3,3,3,3],[3,3,3,3,2,2,2],[3,3,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2]] | ||
} |