递归地添加到切片中

问题描述 投票:0回答:2

我正在尝试在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]]

因此,每次我使用append时,它都会添加新的切片并覆盖所有其他切片。为什么会这样呢?顺便说一句,如果我只是使用全局变量而不是指针,则是相同的。

谢谢

pointers go recursion slice permutation
2个回答
0
投票

您没有将不同的[]int切片附加到较大的[][]int切片中,而是一次又一次地附加了相同的a。并且您要多次修改a。最后,您已经将a修改回原来的样子,这就是为什么最终输出看起来像原始输入listNumbers重复了六次的原因。

这是解决问题的更直接的方法:

package main

import "fmt"

func main() {
  a := []int{1}
  p := [][]int{a, a, a}
  fmt.Println(p) // [[1] [1] [1]]
  a[0] = 2
  fmt.Println(p) // [[2] [2] [2]]
}

为了获得所需的结果,您需要制作a的副本,以后再修改a时不会受到影响。例如:

tmp := make([]int, len(a))
copy(tmp, a)
*p = append(*p, tmp)

阅读有关copy here的更多信息。


0
投票

好吧,在@Amit Kumar Gupta的帮助下,我开始工作了!这是新代码:套餐主

    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)
            tmp := make([]int, len(a)) 
            /*
            'a' is like a pointer to an object,
            every time you modify 'a' it will change all the elemets of 'a'
            in the permutations list.
            like so
            :
            a := []int{1}
            p := [][]int{a, a, a}
            fmt.Println(p) // [[1] [1] [1]]
            a[0] = 2
            fmt.Println(p) // [[2] [2] [2]]
            */
            copy(tmp, a)
            *p = append(*p, tmp)
            fmt.Println(*p)
            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
    }

此代码产生此答案:

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

© www.soinside.com 2019 - 2024. All rights reserved.