1 回答
TA贡献1816条经验 获得超6个赞
正如我在注释中所写的那样,原始代码中的问题是函数中的追加用法。用于传递电流扩展的电流到方法。我已向此函数添加了更多日志记录combinationSum2appendacccandidatecombinationSum2
func combinationSum2(candidates []int, output *[][]int, target int, acc []int, sum int) {
if sum == target {
fmt.Println("Acc:", acc)
*output = append(*output, acc)
fmt.Println("Output:", output)
return
}
if sum > target {
return
}
for i := 0; i < len(candidates); i++ {
extendedAcc := append(acc, candidates[i])
fmt.Printf("Extended: %v %p\n", extendedAcc, extendedAcc)
combinationSum2(candidates[i:], output, target, extendedAcc, sum+candidates[i])
}
}
并收到以下结果(这只是前几行有趣的几行)
Extended: [2] 0x14000130008
Extended: [2 2] 0x14000130030
Extended: [2 2 2] 0x1400013c020
Extended: [2 2 2 2] 0x1400013c020
Acc: [2 2 2 2]
Output: &[[2 2 2 2]]
Extended: [2 2 2 3] 0x1400013c020
Extended: [2 2 2 5] 0x1400013c020
Extended: [2 2 3] 0x1400013c040
Extended: [2 2 3 3] 0x1400013c040
如您所见,变量在被添加到后仍然具有相同的地址(它们在值之后打印为十六进制)。此地址的最终值是您在 中看到的值。它不是的原因是附加如何在内部工作的结果。如果当前数组中没有足够的空间,它会创建一个新的数组并向其返回一个切片。当您比较 slice 的地址时,此行为是可见的。extendedAccOutput[2 2 2 5]Output[2 2 3 3]extended
这是正常工作的解决方案:
package main
import "fmt"
type node struct {
value int
children []*node
}
func combinationSum(candidates []int, target int) [][]int {
var output [][]int
var acc []int
combinationSum2(candidates, &output, target, acc, 0)
return output
}
func combinationSum2(candidates []int, output *[][]int, target int, acc []int, sum int) {
if sum == target {
fmt.Println(acc)
*output = append(*output, acc)
fmt.Println(output)
return
}
if sum > target {
return
}
for i := 0; i < len(candidates); i++ {
currentAcc := make([]int, 0, len(acc) + 1)
currentAcc = append(currentAcc, acc...)
currentAcc = append(currentAcc, candidates[i])
combinationSum2(candidates[i:], output, target, currentAcc, sum+candidates[i])
}
}
func main() {
combinationSum([]int{2, 3, 5}, 8)
}
或者,该函数可能如下所示:combinationSum2
func combinationSum2(candidates []int, output *[][]int, target int, acc []int, sum int) {
if sum == target {
fmt.Println(acc)
accCopy := make([]int, 0, len(acc))
accCopy = append(accCopy, acc...)
*output = append(*output, accCopy)
fmt.Println(output)
return
}
if sum > target {
return
}
for i := 0; i < len(candidates); i++ {
combinationSum2(candidates[i:], output, target, append(acc, candidates[i]), sum+candidates[i])
}
}
在我看来,这通常不太安全,但可以很好地解决这个问题。
- 1 回答
- 0 关注
- 52 浏览
添加回答
举报