Merge pull request #240 from QuinnDK/添加0070组合优化Go版本

添加0070组合优化Go版本
This commit is contained in:
Carl Sun
2021-05-24 18:04:03 +08:00
committed by GitHub
2 changed files with 26 additions and 2 deletions

View File

@ -436,8 +436,6 @@ func backtrack(n,k,start int,track []int){
``` ```
----------------------- -----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321) * B站视频[代码随想录](https://space.bilibili.com/525438321)

View File

@ -193,6 +193,32 @@ class Solution:
return res return res
``` ```
Go Go
```Go
var res [][]int
func combine(n int, k int) [][]int {
res=[][]int{}
if n <= 0 || k <= 0 || k > n {
return res
}
backtrack(n, k, 1, []int{})
return res
}
func backtrack(n,k,start int,track []int){
if len(track)==k{
temp:=make([]int,k)
copy(temp,track)
res=append(res,temp)
}
if len(track)+n-start+1 < k {
return
}
for i:=start;i<=n;i++{
track=append(track,i)
backtrack(n,k,i+1,track)
track=track[:len(track)-1]
}
}
```