我尝试在每次迭代后将切片sofar
添加到matrix
中的新行。
func combinations(sofar []int, rest []int, n int, matrix [][]int, count int) {
if n == 0 {
//Next two lines problematic
matrix[count] = append(matrix[count], sofar[0], sofar[1], sofar[2])
count++
fmt.Println(sofar)
} else {
for i := range rest[:len(rest)] {
concat := sofar
concat = append(concat, rest[i])
combinations(concat, rest[i+1:], n-1, matrix, count)
}
}
}
func factorial(x uint) uint {
if x == 0 {
return 1
}
return x * factorial(x-1)
}
驱动程序
func triangleNumber() int {
sofar := []int{}
rest := []int{1,2,3,4}
matrixSize := factorial(4)/(factorial(1)*factorial(3))
matrix := make([][]int, matrixSize)
count := 0
fmt.Println(matrixSize)
combinations(sofar, rest, 3, matrix, count)
return 0
}
triangleNumber()
我想要matrix
;
[1 2 3]
[1 2 4]
[1 3 4]
[2 3 4]
但相反,这一切都在第一行。还有一种方法可以摆脱count
,只需将切片sofar
添加到下一行吗?
答案 0 :(得分:1)
您要将所有内容添加到第一行,并且您需要添加到下一行,请参阅:
试试这个(对代码进行最小的更改:使count
成为指针):
package main
import (
"fmt"
)
func main() {
triangleNumber()
}
func triangleNumber() int {
sofar := []int{}
rest := []int{1, 2, 3, 4}
matrixSize := factorial(4) / (factorial(1) * factorial(3))
matrix := make([][]int, matrixSize)
count := 0
fmt.Println(matrixSize)
combinations(sofar, rest, 3, matrix, &count)
fmt.Println(matrix)
return 0
}
func combinations(sofar []int, rest []int, n int, matrix [][]int, count *int) {
if n == 0 {
//Next two lines problematic
matrix[*count] = append(matrix[*count], sofar[0], sofar[1], sofar[2])
*count++
// fmt.Println(count, sofar)
} else {
for i := range rest[:len(rest)] {
concat := sofar
concat = append(concat, rest[i])
combinations(concat, rest[i+1:], n-1, matrix, count)
}
}
}
func factorial(x uint) uint {
if x == 0 {
return 1
}
return x * factorial(x-1)
}
输出:
4
[[1 2 3] [1 2 4] [1 3 4] [2 3 4]]
另外,对于您的特殊情况,这也适用:
matrix[*count] = []int{sofar[0], sofar[1], sofar[2]}
而不是:
matrix[*count] = append(matrix[*count], sofar[0], sofar[1], sofar[2])
答案 1 :(得分:1)
实际上,我对你的程序有几点注意事项:
combinations
函数需要将其返回,以便在添加未来元素(整数片段)时,它们实际上就在那里。我已经添加了一些调试并对程序进行了一些改造,看看是否有意义:
package main
import (
"fmt"
)
func main() {
triangleNumber()
}
func combinations(sofar []int, rest []int, n int, matrix [][]int, count int) [][]int {
fmt.Println("Entered with matrix", matrix)
if n == 0 {
fmt.Println("Entered with count", count)
//Next two lines problematic
matrix = append(matrix, sofar)
count++
fmt.Println(sofar)
fmt.Println("Printing matrix\n***", matrix, "\n***")
return matrix
} else {
for i := range rest[:len(rest)] {
concat := sofar
concat = append(concat, rest[i])
matrix = combinations(concat, rest[i+1:], n-1, matrix, count)
fmt.Println("Sending with count", count)
}
}
return matrix
}
func factorial(x uint) uint {
if x == 0 {
return 1
}
return x * factorial(x-1)
}
func triangleNumber() int {
sofar := []int{}
rest := []int{1, 2, 3, 4}
matrixSize := factorial(4) / (factorial(1) * factorial(3))
matrix := make([][]int, 0)
count := 0
fmt.Println(matrixSize)
combinations(sofar, rest, 3, matrix, count)
return 0
}
正如你所看到的,你可以用这种方法摆脱count
(看看输出)。虽然还有一些改进的余地,但我想这可以解决你的问题。
在操场上:https://play.golang.org/p/rnCdPcaIG3N
希望这有帮助。