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