递归地附加到切片切片

我正在尝试在 Go 中实现一个简单的函数,该函数返回一组数字的所有排列。我让它打印所有排列,但我不能让它将它们附加到 2D 切片。这是排列的代码:


package main


import "fmt"


// Generating permutation using Heap Algorithm

func heapPermutation(p *[][]int, a []int, size, n, count int) int {

    count++

    // if size becomes 1 then prints the obtained

    // permutation

    if size == 1 {

        fmt.Println(a)

        *p = append(*p, a)

        return count

    }

    i := 0

    for i < size {

        count = heapPermutation(p, a, size-1, n, count)


        // if size is odd, swap first and last

        // element

        // else If size is even, swap ith and last element

        if size%2 != 0 {

            a[0], a[size-1] = a[size-1], a[0]

        } else {

            a[i], a[size-1] = a[size-1], a[i]

        }

        i++

    }

    return count

}

这是主要功能:


func main() {

    listNumbers := []int{1, 2, 3}

    n := len(listNumbers)

    permutations := make([][]int, 0)

    p := &permutations

    heapPermutation(p, listNumbers, n, n, 0)

    fmt.Print(permutations)

}

当我运行此代码时,我得到以下输出:


[1 2 3]

[2 1 3]

[3 1 2]

[1 3 2]

[2 3 1]

[3 2 1]

[[1 2 3] [1 2 3] [1 2 3] [1 2 3] [1 2 3] [1 2 3]]

因此,您可以看到该函数能够找到排列,但是当我尝试附加它时会发生一些奇怪的事情。如果我fmt.Println(*p)在每个附加之前添加一个,我会得到这个结果:


[1 2 3]

[[1 2 3]]

[2 1 3]

[[2 1 3] [2 1 3]]

[3 1 2]

[[3 1 2] [3 1 2] [3 1 2]]

[1 3 2]

[[1 3 2] [1 3 2] [1 3 2] [1 3 2]]

[2 3 1]

[[2 3 1] [2 3 1] [2 3 1] [2 3 1] [2 3 1]]

[3 2 1]

[[3 2 1] [3 2 1] [3 2 1] [3 2 1] [3 2 1] [3 2 1]]

[[1 2 3] [1 2 3] [1 2 3] [1 2 3] [1 2 3] [1 2 3]]

所以看起来每次我使用追加时,它都会添加新切片并覆盖所有其他切片。为什么会这样?顺便说一句,如果我只使用全局变量而不是指针,那也是一样的。


人到中年有点甜
浏览 109回答 2
2回答

白衣染霜花

您不会将不同[]int的切片附加到更大的切片中,而是一遍又一遍地[][]int附加相同的切片 ( )。a而且您正在修改a几次。到最后,你已经修改a回原来的样子,这就是为什么你的最终输出看起来像原来的输入listNumbers重复了六次。这是查看问题的更直接的方法:package mainimport "fmt"func main() {&nbsp; a := []int{1}&nbsp; p := [][]int{a, a, a}&nbsp; fmt.Println(p) // [[1] [1] [1]]&nbsp; a[0] = 2&nbsp; fmt.Println(p) // [[2] [2] [2]]}为了获得您想要的结果,您需要制作副本,a这些副本在以后修改时不会受到影响a。例如:tmp := make([]int, len(a))copy(tmp, a)*p = append(*p, tmp)阅读更多关于copy 这里。

哔哔one

好的,在@Amit Kumar Gupta 的帮助下,我成功了!这是新代码:package main&nbsp; &nbsp; import "fmt"&nbsp; &nbsp; // Generating permutation using Heap Algorithm&nbsp; &nbsp; func heapPermutation(p *[][]int, a []int, size, n, count int) int {&nbsp; &nbsp; &nbsp; &nbsp; count++&nbsp; &nbsp; &nbsp; &nbsp; // if size becomes 1 then prints the obtained&nbsp; &nbsp; &nbsp; &nbsp; // permutation&nbsp; &nbsp; &nbsp; &nbsp; if size == 1 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(a)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmp := make([]int, len(a))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'a' is like a pointer to an object,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; every time you modify 'a' it will change all the elemets of 'a'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; in the permutations list.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; like so&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a := []int{1}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p := [][]int{a, a, a}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(p) // [[1] [1] [1]]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a[0] = 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(p) // [[2] [2] [2]]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; copy(tmp, a)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *p = append(*p, tmp)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(*p)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return count&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; i := 0&nbsp; &nbsp; &nbsp; &nbsp; for i < size {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count = heapPermutation(p, a, size-1, n, count)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if size is odd, swap first and last&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // element&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // else If size is even, swap ith and last element&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if size%2 != 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a[0], a[size-1] = a[size-1], a[0]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a[i], a[size-1] = a[size-1], a[i]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return count&nbsp; &nbsp; }这段代码产生了这个答案:[[1 2 3] [2 1 3] [3 1 2] [1 3 2] [2 3 1] [3 2 1]]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go