第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將數(shù)組附加到矩陣時的奇怪行為

將數(shù)組附加到矩陣時的奇怪行為

Go
藍山帝景 2022-09-19 10:19:55
我錯過了一些關(guān)于切片的基本內(nèi)容,導(dǎo)致我的結(jié)果最終看起來很奇怪。是的,這是來自利特代碼的問題。我用它來學(xué)習(xí)圍棋,因為我發(fā)現(xiàn)用新語言解決算法對我很有幫助。我不需要算法的答案,也不需要知道如何修復(fù)算法。我只想知道為什么當(dāng)我附加另一個值時,我的附加值會發(fā)生變化。首先,這是我的代碼: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++ {        combinationSum2(candidates[i:], output, target, append(acc, candidates[i]), sum + candidates[i])    }    }我正在測試這個代碼,候選者=[2,3,5]和目標(biāo)=8正確的輸出應(yīng)該是[[2,2,2,2],[2,3,3],[3,5]];但是,我的代碼返回 [[2,2,2,5],[2,3,3],[3,5]]有趣的是,當(dāng)我記錄if語句中的 acc 值和追加 acc 值后的輸出時,似乎我追加的值在追加第二個數(shù)組后發(fā)生了變化。acc = [2 2 2 2]output = &[[2 2 2 2]]acc = [2 3 3]output = &[[2 2 2 5] [2 3 3]]acc = [3 5]output = &[[2 2 2 5] [2 3 3] [3 5]]我嘗試在本地運行它,并得到同樣的奇怪行為。這是什么原因造成的?
查看完整描述

1 回答

?
瀟湘沐

TA貢獻1816條經(jīng)驗 獲得超6個贊

正如我在注釋中所寫的那樣,原始代碼中的問題是函數(shù)中的追加用法。用于傳遞電流擴展的電流到方法。我已向此函數(shù)添加了更多日志記錄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])

    }

}

并收到以下結(jié)果(這只是前幾行有趣的幾行)


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

如您所見,變量在被添加到后仍然具有相同的地址(它們在值之后打印為十六進制)。此地址的最終值是您在 中看到的值。它不是的原因是附加如何在內(nèi)部工作的結(jié)果。如果當(dāng)前數(shù)組中沒有足夠的空間,它會創(chuàng)建一個新的數(shù)組并向其返回一個切片。當(dāng)您比較 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)

}

或者,該函數(shù)可能如下所示: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])

    }

}

在我看來,這通常不太安全,但可以很好地解決這個問題。


查看完整回答
反對 回復(fù) 2022-09-19
  • 1 回答
  • 0 關(guān)注
  • 65 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號