题目:
题解:
func permute(nums []int) [][]int {
var (
n = len(nums)
dfs func(vals []int) // 已选择数 排列为vals 后续回溯继续选择 直至选完
ans [][]int
)
dfs = func(vals []int) {
//边界
if len(vals) == n {
ans = append(ans, vals)
}
//转移 枚举选哪个
for _, x := range nums {
if !slices.Contains(vals, x) { // 不包含才能选
var valsTmp = make([]int, len(vals))
copy(valsTmp, vals)
valsTmp = append(valsTmp, x) // 选
dfs(valsTmp)
}
}
}
dfs([]int{})
return ans
}