Source of the topic
https://leetcode.com/problems/subsets/
Given a set of distinct integers, 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: Given a list with no duplicate elements, return all its subsets, noting that the elements in each subset are non-descending, and subsets cannot be duplicated.
Topic ideas
Use the DFS idea to add a number at a time. Because the list is sorted beforehand, the next time DFS should only traverse the element after the current position.
AC Code (PYTHON)
1 classsolution (object):2 defsubsets (Self, nums):3 """4 : Type Nums:list[int]5 : Rtype:list[list[int]]6 """7 defDfs (depth, start, valueList):8 res.append (valueList)9 ifdepth = =Len (nums):Ten return One forIinchRange (Start, Len (nums)): ADFS (depth + 1, i + 1, valuelist+[Nums[i]]) - - Nums.sort () theres = [] - DFS (0, 0, []) - returnRes
[Leetcode] (python): 078 subsets