采坑:过程中使用的是分片,分片是引用拷贝
func permute(nums []int) [][]int {
var data []int
var list [][]int
_, list = getPermute(nums, data, list)
return list
}
func getPermute(nums []int, data []int, list [][]int) ([]int, [][]int) {
for indx, num := range nums {
if len(nums) == 1 {
// 分片是引用 需要拷贝值写入list
d := make([]int, len(data))
copy(d, data)
list = append(list, append(d, nums...))
} else if indx == len(nums)-1 {
data = append(data, num)
formNums := make([]int, len(nums))
copy(formNums, nums)
newNums := append(formNums[:indx])
data, list = getPermute(newNums, data, list)
} else {
data = append(data, num)
// 原列表不能改变
formNums := make([]int, len(nums))
copy(formNums, nums)
newNums := append(formNums[:indx], formNums[indx+1:]...)
data, list = getPermute(newNums, data, list)
}
}
if len(data) > 0 {
data = data[:len(data)-1]
}
return data, list
}