为什么我的范围在切片中显示不存在的值?

我试图解决 Go 中的 Leetcode 问题。问题是子集。


这是我用一些调试日志编写的整个代码:



package main


import (

    "fmt"

)


func main() {

    v := []int{9, 0, 3, 5, 7}

    fmt.Println(subsets(v))

}


func subsets(nums []int) [][]int {

    result := [][]int{

        []int{}, // empty

    }


    for _, num := range nums {  

        fmt.Println("==========")

        fmt.Println(num)

        fmt.Printf("result = %v\n", result)


        temp := [][]int{}

        for _, r := range result {

            fmt.Printf("r = %v\n", r)

            temp = append(temp, append(r, num))

        }


        for _, t := range temp {

            result = append(result, t)

        }


        fmt.Println("==========")       

    }


    return result

}

(我还准备了Go play ground URL)


这是上面代码的输出:



==========

9

result = [[]]

r = []

==========

==========

0

result = [[] [9]]

r = []

r = [9]

==========

==========

3

result = [[] [9] [0] [9 0]]

r = []

r = [9]

r = [0]

r = [9 0]

==========

==========

5

result = [[] [9] [0] [9 0] [3] [9 3] [0 3] [9 0 3]]

r = []

r = [9]

r = [0]

r = [9 0]

r = [3]

r = [9 3]

r = [0 3]

r = [9 0 3]

==========

==========

7

result = [[] [9] [0] [9 0] [3] [9 3] [0 3] [9 0 3] [5] [9 5] [0 5] [9 0 5] [3 5] [9 3 5] [0 3 5] [9 0 3 5]] // (a)

r = []

r = [9]

r = [0]

r = [9 0]

r = [3]

r = [9 3]

r = [0 3]

r = [9 0 3]

r = [5]

r = [9 5]

r = [0 5]

r = [9 0 5]

r = [3 5]

r = [9 3 5]

r = [0 3 5]

r = [9 0 3 7] // (b)

==========

[[] [9] [0] [9 0] [3] [9 3] [0 3] [9 0 3] [5] [9 5] [0 5] [9 0 5] [3 5] [9 3 5] [0 3 5] [9 0 3 7] [7] [9 7] [0 7] [9 0 7] [3 7] [9 3 7] [0 3 7] [9 0 3 7] [5 7] [9 5 7] [0 5 7] [9 0 5 7] [3 5 7] [9 3 5 7] [0 3 5 7] [9 0 3 7 7]]

让我们看看第5个result。(我在那里指出(a))截至那里,最后一个元素result是[9, 0, 3, 5],这是预期的行为。但是,在那之后,当我尝试在 中写入元素的调试日志时result,它会更改为[9, 0, 3, 7]( (b))。


你知道为什么吗?


回首忆惘然
浏览 102回答 1
1回答

qq_花开花谢_0

append需要时更改r:而不是: temp = append(temp, append(r, num))您可以使用:            rr := make([]int, len(r))            copy(rr, r)            rr = append(rr, num)            temp = append(temp, rr)你很高兴:package mainimport (    "fmt")func main() {    v := []int{9, 0, 3, 5, 7}    fmt.Println(subsets(v))}func subsets(nums []int) [][]int {    result := [][]int{        []int{}, // empty    }    for _, num := range nums {        fmt.Println("==========")        fmt.Println(num)        fmt.Printf("result = %v\n", result)        temp := [][]int{}        for _, r := range result {            fmt.Printf("r = %v\n", r)            // append(r, num)            rr := make([]int, len(r))            copy(rr, r)            rr = append(rr, num)            temp = append(temp, rr)        }        for _, t := range temp {            result = append(result, t)        }        fmt.Println("==========")    }    return result}只需调试您的代码,您就会看到,它发生在这里:        temp := [][]int{}        for _, r := range result {            fmt.Println("result =", result, len(result), cap(result))            fmt.Println("r ==", r, len(r), cap(r))            fmt.Println("num =", num)            rr := append(r, num)            fmt.Println("r ==", r, len(r), cap(r))            fmt.Println("rr ==", rr, len(rr), cap(rr))            fmt.Println("result =", result, len(result), cap(result))            fmt.Println("temp =", temp)            temp = append(temp, rr)            fmt.Println("temp =", temp)        }什么时候result = [[] [9] [0] [9 0] [3] [9 3] [0 3] [9 0 3] [5] [9 5] [0 5] [9 0 5] [3 5] [9 3 5] [0 3 5] [9 0 3 5]] 16 16r == [9 0 3] 3 4num = 7r == [9 0 3] 3 4rr == [9 0 3 7] 4 4result = [[] [9] [0] [9 0] [3] [9 3] [0 3] [9 0 3] [5] [9 5] [0 5] [9 0 5] [3 5] [9 3 5] [0 3 5] [9 0 3 7]] 16 16因为: append(r, num)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go