leetcode刷题笔记(Golang)--90. Subsets II

90. Subsets II

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)
	}
}
发布了65 篇原创文章 · 获赞 0 · 访问量 333

猜你喜欢

转载自blog.csdn.net/weixin_44555304/article/details/104365422
今日推荐