Skip to content

Commit 9f21369

Browse files
authored
Create 78_Subsets.md
1 parent 29cc4c0 commit 9f21369

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

78_Subsets.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
```

0 commit comments

Comments
 (0)