递归地附加到切片切片 [英] appending to slice of slices recursively

查看:64
本文介绍了递归地附加到切片切片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to implement a simple function in Go that returns all permutations of a set of numbers. I got it to print all of the permutations, but I cant get it to append those to a 2D slice. this is the code for the permutations:

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)
}

当我运行这段代码时,我得到了这个输出:

when I run this code I get this output:

[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) ,我会得到这个结果:

So you can see that the function is able to find the permutations but something weird happens when I try to append it. If I add a fmt.Println(*p) before each append I get this result:

[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 时,它都会添加新切片并覆盖所有其他切片.为什么会这样?顺便说一句,如果我只使用一个全局变量而不是一个指针也是一样的.

So it looks like every time I use append, it adds the new slice and overwrites all the other slices. Why does that happened? By the way, it's the same if I just use a global variable instead of a pointer.

谢谢

推荐答案

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

You're not appending different []int slices into the bigger [][]int slice, you're appending the same one (a) over and over again. And you're modifying a several times. By the end, you've modified a back to what it originally was, which is why your final output looks like the original input listNumbers repeated six times.

以下是查看问题的更直接的方法:

Here's a more straightforward way to see the problem:

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 时不会受到影响.例如:

To get your desired result, you need to make copies of a which don't get affected later when you subsequently modify a. E.g.:

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

阅读有关 copy 的更多信息 这里.

Read more about copy here.

这篇关于递归地附加到切片切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆