go
实现全排列组合
package main
import "fmt"
func demo(src []int) {
for i := 0; i < len(src); i++ {
first := src[i]
temp := make([]int, 0)
temp = append(temp, src[:i]...)
temp = append(temp, src[i+1:]...)
order([]int{first}, temp)
}
}
func order(s []int, src []int) {
if len(src) <= 1 {
fmt.Println(s, src)
return
}
firt := src[0]
for i := 0; i < len(src); i++ {
firt, src[i] = src[i], firt
temp := make([]int, 0)
temp = append(temp, src[1:]...)
s_demo := make([]int, len(s))
s_demo = s
dd := make([]int, 0)
dd = append(s_demo, firt)
order(dd, temp)
}
}
func main() {
demo([]int{1, 2, 3, 4})
}