-
Notifications
You must be signed in to change notification settings - Fork 1
/
arrayPermutations.go
66 lines (58 loc) · 1.4 KB
/
arrayPermutations.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
60
61
62
63
64
65
66
package array
/*
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]
]
Solution using recursion:
First have a function called permutate that accepts an array and 2 indexes start and end.
The base case is when start == end then we just return the array.
In any other case we iterate from start to end and each time we swap the start with i we
call recursively permutate and restore the swap:
[1,2,3] -> start=0, end=2 keeps 1 fixed
[1,2,3] -> start=1, end=2 keeps 2 fixed
[1,2,3] -> start=2, end=2 return [1,2,3]
[1,2,3] -> restore 2nd swap
[1,3,2] -> swap 3 for 2
[1,3,2] -> start=2, end=2 return [1,3,2]
[1,2,3] -> restore 2nd swap
[1,2,3] -> restore 1nd swap
[2,1,3] -> swap 2 for 1
...
*/
func Permute(nums []int) [][]int {
result := [][]int{}
permutateHelper(nums, 0, len(nums), func(arr []int) {
result = append(result, arr)
})
return result
}
func permutateHelper(arr []int, start, end int, cb func(arr []int)) {
if start == end {
tmp := make([]int, len(arr))
copy(tmp, arr)
cb(tmp)
} else {
for i := start;i<end; i+=1 {
// Keep current number fixed
swap(arr, start, i)
// Perform recursively
permutateHelper(arr, start+1, end, cb)
// Restore swap
swap(arr, start, i)
}
}
}
func swap(arr []int, i, j int) {
temp := arr[i]
arr[i] = arr[j]
arr[j] = temp
}