-
Notifications
You must be signed in to change notification settings - Fork 20
/
SubsetsII.go
101 lines (85 loc) · 1.6 KB
/
SubsetsII.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package SubsetsII
import (
"sort"
)
//Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
//
//Note: The solution set must not contain duplicate subsets.
//
//For example,
//If nums = [1,2,2], a solution is:
//
//[
//[2],
//[1],
//[1,2,2],
//[2,2],
//[1,2],
//[]
//]
//
//Accepted.
func subsetsWithDup(nums []int) [][]int {
if nums == nil || len(nums) == 0 {
return [][]int{}
}
lists := make([][]int, 0)
if len(nums) == 1 {
lists = append(lists, []int{})
lists = append(lists, []int{nums[0]})
return lists
}
sort.Ints(nums)
for _, list := range subsetsWithDup(nums[:len(nums)-1]) {
var l []int = nil
l = append(l, nums[len(nums)-1])
for _, integer := range list {
l = append(l, integer)
}
equalsL := false
equalsList := false
for _, li := range lists {
if sameIntSlice(list, li) {
equalsList = true
}
if sameIntSlice(li, l) {
equalsL = true
}
if equalsList && equalsL {
break
}
}
if !equalsL {
lists = append(lists, l)
}
if !equalsList {
lists = append(lists, list)
}
}
return lists
}
func sameIntSlice(x, y []int) bool {
if len(x) != len(y) {
return false
}
// create a map of int -> int
diff := make(map[int]int, len(x))
for _, _x := range x {
// 0 value for int is 0, so just increment a counter for the int
diff[_x]++
}
for _, _y := range y {
// If the string _y is not in diff bail out early
if _, ok := diff[_y]; !ok {
return false
}
diff[_y] -= 1
if diff[_y] == 0 {
delete(diff, _y)
}
}
if len(diff) == 0 {
return true
}
return false
}