-
Notifications
You must be signed in to change notification settings - Fork 0
/
046.go
59 lines (51 loc) · 925 Bytes
/
046.go
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package p046
import (
"sort"
)
/**
Given a collection of distinct numbers, return all possible permutations.
For example,
[1,2,3] have the following permutations:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
*/
// 字典序全排列生成算法,更快的还有邻位对换法
func permute(nums []int) [][]int {
ans := make([][]int, 0)
sort.Ints(nums)
lenn := len(nums)
nextPermute := func() bool {
onepermute := make([]int, lenn)
copy(onepermute, nums)
ans = append(ans, onepermute)
for i := lenn - 2; i >= 0; i-- {
if nums[i] < nums[i+1] {
j := 0
for j = lenn - 1; j > i; j-- {
if nums[j] > nums[i] {
nums[i], nums[j] = nums[j], nums[i]
break
}
}
i = i + 1
j = lenn - 1
for i < j {
nums[i], nums[j] = nums[j], nums[i]
i++
j--
}
return true
}
}
return false
}
for nextPermute() {
}
return ans
}