permutation
permutation.go 源码
package main
//全排列
func permute(nums []int) [][]int {
var res [][]int
var data []int
permuteHelper(len(nums), nums, data, &res)
return res
}
func permuteHelper(n int, nums []int, data []int, res *[][]int) {
if len(data) == n {
temp := make([]int, n)
copy(temp, data)
*res = append(*res, temp)
return
}
for _, v := range nums {
if exist(data, v) {
continue
}
data = append(data, v)
permuteHelper(n, nums, data, res)
data = data[:len(data)-1]
}
}
func exist(data []int, k int) bool {
for _, v := range data {
if v == k {
return true
}
}
return false
}
你可能感兴趣的文章
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦