mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
update 0039.组合总和:修改错字和更改 go 代码
This commit is contained in:
@ -42,7 +42,7 @@ candidates 中的数字可以无限制重复被选取。
|
|||||||
|
|
||||||
题目中的**无限制重复被选取,吓得我赶紧想想 出现0 可咋办**,然后看到下面提示:1 <= candidates[i] <= 200,我就放心了。
|
题目中的**无限制重复被选取,吓得我赶紧想想 出现0 可咋办**,然后看到下面提示:1 <= candidates[i] <= 200,我就放心了。
|
||||||
|
|
||||||
本题和[77.组合](https://programmercarl.com/0077.组合.html),[216.组合总和III](https://programmercarl.com/0216.组合总和III.html)和区别是:本题没有数量要求,可以无限重复,但是有总和的限制,所以间接的也是有个数的限制。
|
本题和[77.组合](https://programmercarl.com/0077.组合.html),[216.组合总和III](https://programmercarl.com/0216.组合总和III.html)的区别是:本题没有数量要求,可以无限重复,但是有总和的限制,所以间接的也是有个数的限制。
|
||||||
|
|
||||||
本题搜索的过程抽象成树形结构如下:
|
本题搜索的过程抽象成树形结构如下:
|
||||||
|
|
||||||
@ -335,33 +335,32 @@ class Solution:
|
|||||||
主要在于递归中传递下一个数字
|
主要在于递归中传递下一个数字
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
var (
|
||||||
|
res [][]int
|
||||||
|
path []int
|
||||||
|
)
|
||||||
func combinationSum(candidates []int, target int) [][]int {
|
func combinationSum(candidates []int, target int) [][]int {
|
||||||
var trcak []int
|
res, path = make([][]int, 0), make([]int, 0, len(candidates))
|
||||||
var res [][]int
|
sort.Ints(candidates) // 排序,为剪枝做准备
|
||||||
backtracking(0,0,target,candidates,trcak,&res)
|
dfs(candidates, 0, target)
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
func backtracking(startIndex,sum,target int,candidates,trcak []int,res *[][]int){
|
|
||||||
//终止条件
|
func dfs(candidates []int, start int, target int) {
|
||||||
if sum==target{
|
if target == 0 { // target 不断减小,如果为0说明达到了目标值
|
||||||
tmp:=make([]int,len(trcak))
|
tmp := make([]int, len(path))
|
||||||
copy(tmp,trcak)//拷贝
|
copy(tmp, path)
|
||||||
*res=append(*res,tmp)//放入结果集
|
res = append(res, tmp)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if sum>target{return}
|
for i := start; i < len(candidates); i++ {
|
||||||
//回溯
|
if candidates[i] > target { // 剪枝,提前返回
|
||||||
for i:=startIndex;i<len(candidates);i++{
|
break
|
||||||
//更新路径集合和sum
|
}
|
||||||
trcak=append(trcak,candidates[i])
|
path = append(path, candidates[i])
|
||||||
sum+=candidates[i]
|
dfs(candidates, i, target - candidates[i])
|
||||||
//递归
|
path = path[:len(path) - 1]
|
||||||
backtracking(i,sum,target,candidates,trcak,res)
|
|
||||||
//回溯
|
|
||||||
trcak=trcak[:len(trcak)-1]
|
|
||||||
sum-=candidates[i]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user