Source of the topic
https://leetcode.com/problems/subsets-ii/
Given a collection of integers that might contain duplicates, Nums, return all possible subsets.
Note:
- Elements in a subset must is in non-descending order.
- The solution set must not contain duplicate subsets.
Test instructions Analysis
Input:
: Type Nums:list[int]
Output:
: Rtype:list[list[int]]
Conditions: Returns all the subsets of a list, noting that the elements in the list can be duplicates, but that the subset returned is not duplicated, and the order of the elements in the sub-set is non-descending.
Topic ideas
With DFS, with 78 questions, just when adding a subset, determine whether the subset already exists and then increase.
Note: When you extend a subset, you can always add an element, and sort the list first, and only the elements after the current position are iterated each time, so you can avoid duplication.
AC Code (PYTHON)
1 classsolution (object):2 defsubsetswithdup (Self, nums):3 """4 : Type Nums:list[int]5 : Rtype:list[list[int]]6 """7 defDfs (depth, start, valueList):8 ifValueList not inchRes:9 res.append (valueList)Ten ifdepth = =Len (nums): One return A forIinchRange (Start, Len (nums)): -DFS (depth + 1, i + 1, valuelist+[Nums[i]]) - the Nums.sort () -res = [] - DFS (0, 0, []) - returnRes
[Leetcode] (python): 090 subsets II