- Given a collection of distinct integers, return all possible permutations.
- Example:
- Input: [1,2,3]
- Output:
- [
- [1,2,3],
- [1,3,2],
- [2,1,3],
- [2,3,1],
- [3,1,2],
- [3,2,1]
- ]
- package main
- import (
- "fmt"
- )
- func swap(nums []int, i, j int) {
- nums[i], nums[j] = nums[j], nums[i]
- }
- func permute(nums []int) [][]int {
- length := len(nums) - 1
- var res [][]int
- var n []int
- dfs1(nums, 0, length, &n)
- for m := 0; m < len(n); m = m + len(nums) {
- res = append(res, n[m:m+len(nums)])
- }
- fmt.Println(res)
- return res
- }
- func dfs1(nums []int, cur, end int, res *[]int) {
- if cur == end {
- *res = append(*res, nums...)
- return
- } else {
- for i := cur; i <= end; i++ {
- swap(nums, cur, i)
- dfs1(nums, cur+1, end, res)
- swap(nums, cur, i)
- }
- }
- }
来源: http://www.bubuko.com/infodetail-3001104.html