题目:
题解:
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
}