重複を含む可能性のある整数、NUMSのコレクションを考えると、すべての可能なサブセット(パワーセット)を返します。
注:ソリューション・セットは、重複サブセットを含めることはできません。
例:
入力:[1,2,2]
出力
:
[ [2]、
[1]、
[1,2,2]、
[2,2]、
[1,2]、
[]
]
func subsetsWithDup(nums []int) [][]int {
res := [][]int{}
lg := len(nums)
sort.Ints(nums)
for i := 0; i <= lg; i++ {
dfs(nums, 0, i, []int{}, &res)
}
return res
}
func dfs(graph []int, src int, target int, path []int, paths *[][]int) {
if len(path) == target {
tmp := make([]int, target)
copy(tmp, path)
*paths = append(*paths, tmp)
}
for i := src; i < len(graph); i++ {
if i > src && graph[i] == graph[i-1] {
continue
}
dfs(graph, i+1, target, append(path, graph[i]), paths)
}
}