-
Notifications
You must be signed in to change notification settings - Fork 21
/
Permutations.go
45 lines (38 loc) 路 775 Bytes
/
Permutations.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
package Permutations
//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]
//]
//
//Accepted.
func permute(nums []int) [][]int {
results := make([][]int, 0)
length := len(nums)
if length == 0 {
return results
}
if length == 1 {
results = append(results, []int{nums[0]})
return results
}
ints := nums[0:length-1]
for _, l := range permute(ints) {
for i := 0; i < len(l)+1; i++ {
tmp := make([]int, 0)
tmp = append(tmp, l...)
rear := append([]int{}, tmp[i:]...)
tmp = append(tmp[0:i], nums[length-1])
tmp = append(tmp, rear...)
results = append(results, tmp)
}
}
return results
}