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.
Example:
Input: [1,2,2]
Output:
[
[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)
}
}