1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// @Title: 全排列 (Permutations)
// @Author: 15816537946@163.com
// @Date: 2020-11-16 14:36:00
// @Runtime: 4 ms
// @Memory: 2.6 MB
func permute(nums []int) [][]int {
	res := make([][]int, 0)
	nLen := len(nums)

	var perm func(int)
	perm = func(i int) {
		if i == nLen-1 {
			tmp := make([]int, nLen)
			copy(tmp, nums)
			res = append(res, tmp)
			return
		}

		for j := i; j < nLen; j++ {
			nums[i], nums[j] = nums[j], nums[i]
			perm(i + 1)
			nums[i], nums[j] = nums[j], nums[i]
		}
	}
	perm(0)
	return res
}