[英]appending to slice of slices recursively
I am trying to implement a simple function in Go that returns all permutations of a set of numbers.我正在尝试在 Go 中实现一个简单的函数,该函数返回一组数字的所有排列。 I got it to print all of the permutations, but I cant get it to append those to a 2D slice.我让它打印所有排列,但我无法将它们附加到 2D 切片。 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
}
and this is the main function:这是主要功能:
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]]
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:如果我在每个追加之前添加一个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]]
So it looks like every time I use append, it adds the new slice and overwrites all the other slices.所以看起来每次我使用 append 时,它都会添加新切片并覆盖所有其他切片。 Why does that happened?为什么会这样? By the way, it's the same if I just use a global variable instead of a pointer.顺便说一下,如果我只使用一个全局变量而不是一个指针也是一样的。
Thanks谢谢
You're not appending different []int
slices into the bigger [][]int
slice, you're appending the same one ( a
) over and over again.您不是将不同的[]int
切片附加到更大的[][]int
切片中,而是一遍又一遍地附加相同的 ( a
) 切片。 And you're modifying a
several times.你修改a
几次。 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.最后,您已将a
修改回原来的样子,这就是为什么您的最终输出看起来像原始输入listNumbers
重复了六次的原因。
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]]
}
To get your desired result, you need to make copies of a
which don't get affected later when you subsequently modify a
.为了让您期望的结果,你需要做的拷贝a
不得到后来的影响,当你随后修改a
。 Eg:例如:
tmp := make([]int, len(a))
copy(tmp, a)
*p = append(*p, tmp)
Ok so by the help of @Amit Kumar Gupta, I got it working!好的,在@Amit Kumar Gupta 的帮助下,我开始工作了! this is the new code: package main这是新代码: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)
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
}
this code produces this answer :这段代码产生了这个答案:
[[1 2 3] [2 1 3] [3 1 2] [1 3 2] [2 3 1] [3 2 1]] [[1 2 3] [2 1 3] [3 1 2] [1 3 2] [2 3 1] [3 2 1]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.