-
Notifications
You must be signed in to change notification settings - Fork 21
/
PermutationsII.go
56 lines (46 loc) 路 1012 Bytes
/
PermutationsII.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
package PermutationsII
import (
"strconv"
)
//Given a collection of numbers that might contain duplicates, return all possible unique permutations.
//
//For example,
//[1,1,2] have the following unique permutations:
//[
//[1,1,2],
//[1,2,1],
//[2,1,1]
//]
//
//Accepted.
func permuteUnique(nums []int) [][]int {
results, numsLength := make([][]int, 0), len(nums)
if numsLength == 0 {
return results
}
if numsLength == 1 {
l := make([]int, 0)
l = append(l, nums[0])
results = append(results, l)
return results
}
ints := append([]int{}, nums[0:numsLength-1]...)
dict := map[string][]int{}
for _, l := range permuteUnique(ints) {
for i := 0; i <= len(l); i++ {
tmp := append([]int{}, l...)
rear := append([]int{}, tmp[i:]...)
tmp = append(tmp[0:i], nums[numsLength-1])
tmp = append(tmp, rear...)
key := ""
for _, i := range tmp {
key += strconv.Itoa(i)
}
dict[key] = tmp
}
}
for _, v := range dict {
results = append(results, v)
}
return results
}