• 78.子集
  • 90.子集II
  • 491.递增子序列
    • 和子集问题很像

子集 II

var (
	res [][]int
	curRes []int
	theNums []int
)
 
func subsetsWithDup(nums []int) [][]int {
	res = [][]int{}
	curRes = []int{}
	theNums = nums
	// 要去重,先排序
	sort.Slice(theNums, func(a, b int) bool {
		return theNums[a] <= theNums[b]
	})
 
	backtracking(0)
 
	return res
}
 
func backtracking(start int) {
	tmp := make([]int, len(curRes))
	copy(tmp, curRes)
	res = append(res, tmp)
 
	for i := start; i < len(theNums); i++ {
		if i > start && theNums[i] == theNums[i - 1] {
			// 树层去重
			continue
		}
		curRes = append(curRes, theNums[i])
		backtracking(i + 1)
		curRes = curRes[:len(curRes) - 1]
	}
}