-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution46.go
More file actions
37 lines (28 loc) · 740 Bytes
/
solution46.go
File metadata and controls
37 lines (28 loc) · 740 Bytes
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
package solution46
// ============================================================================
// 46, Permutations
// URL: https://leetcode.com/problems/permutations/
// ============================================================================
import "slices"
var perms [][]int
func permute(nums []int) [][]int {
perms = make([][]int, 0)
heapPermutation(nums, len(nums), len(nums))
return perms
}
func heapPermutation(a []int, size int, n int) {
if size == 1 {
if len(a[:n]) >= 1 {
perms = append(perms, slices.Clone(a[:n]))
}
return
}
for i := 0; i < size; i++ {
heapPermutation(a, size-1, n)
if size%2 == 1 {
a[0], a[size-1] = a[size-1], a[0]
} else {
a[i], a[size-1] = a[size-1], a[i]
}
}
}