File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ ## 78. Subsets
2+
3+
4+ Given a set of distinct integers, nums, return all possible subsets (the power set).
5+
6+ Note: The solution set must not contain duplicate subsets.
7+
8+ Example:
9+
10+ Input: nums = [ 1,2,3]
11+ Output:
12+ [
13+ [ 3] ,
14+ [ 1] ,
15+ [ 2] ,
16+ [ 1,2,3] ,
17+ [ 1,3] ,
18+ [ 2,3] ,
19+ [ 1,2] ,
20+ [ ]
21+ ]
22+
23+
24+ ``` python
25+ def subsets (self , nums : List[int ]) -> List[List[int ]]:
26+
27+ result = []
28+ def traverse (build , index ):
29+ if len (nums) == index:
30+ result.append(build)
31+ return
32+
33+ traverse(build, index+ 1 )
34+ traverse(build+ [nums[index]], index+ 1 )
35+
36+
37+ traverse([], 0 )
38+ return result
39+ ```
40+
41+
42+ ```
43+ Runtime: 32 ms, faster than 77.43% of Python3 online submissions for Subsets.
44+ Memory Usage: 14.3 MB, less than 100.00% of Python3 online submissions for Subsets.
45+ ```
You can’t perform that action at this time.
0 commit comments