Skip to content

Commit

Permalink
添加 0039.组合总和 go版本
Browse files Browse the repository at this point in the history
添加 0039.组合总和 go版本
  • Loading branch information
X-shuffle authored Jun 19, 2021
1 parent d73ef90 commit 55d48c0
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions problems/0039.组合总和.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,39 @@ class Solution:
```
Go:


> 主要在于递归中传递下一个数字
```go
func combinationSum(candidates []int, target int) [][]int {
var trcak []int
var res [][]int
backtracking(0,0,target,candidates,trcak,&res)
return res
}
func backtracking(startIndex,sum,target int,candidates,trcak []int,res *[][]int){
//终止条件
if sum==target{
tmp:=make([]int,len(trcak))
copy(tmp,trcak)//拷贝
*res=append(*res,tmp)//放入结果集
return
}
if sum>target{return}
//回溯
for i:=startIndex;i<len(candidates);i++{
//更新路径集合和sum
trcak=append(trcak,candidates[i])
sum+=candidates[i]
//递归
backtracking(i,sum,target,candidates,trcak,res)
//回溯
trcak=trcak[:len(trcak)-1]
sum-=candidates[i]
}

}
```
JavaScript:

```js
Expand Down

0 comments on commit 55d48c0

Please sign in to comment.