Ai
1 Star 0 Fork 0

catyMap/AlgorithmNote

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
combinationSum4.go 612 Bytes
一键复制 编辑 原始数据 按行查看 历史
catyMap 提交于 2021-04-25 08:32 +08:00 . 4/25新题
package main
import (
"fmt"
"sort"
)
func combinationSum4(nums []int , target int ) (res int) {
// 0.可能需要加入剪枝的逻辑
sort.Ints(nums)
trace, book := 0, make(map[int]int)
var depth func()
depth = func() {
if _ , ok := book[trace] ; ok{
res ++
return
}
if trace > target {
return
}
if trace == target {
res ++
return
}
count := res
for i := 0 ; i < len(nums) ; i ++ {
trace += nums[i]
depth()
trace -= nums[i]
}
book[trace] = res - count
}
depth()
return res
}
func main() {
fmt.Println(combinationSum4([]int{1,5,8},10))
}
// 181997601
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/dogemap/algorithm-note.git
git@gitee.com:dogemap/algorithm-note.git
dogemap
algorithm-note
AlgorithmNote
dc486f96f6c1

搜索帮助