-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path78.py
More file actions
26 lines (21 loc) · 651 Bytes
/
Copy path78.py
File metadata and controls
26 lines (21 loc) · 651 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
"""Given an integer array nums of unique elements, return all possible
subsets
(the power set).
The solution set must not contain duplicate subsets. Return the solution in any order.
"""
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
res = []
subset = []
def dfs(i):
if i >= len(nums):
res.append(subset.copy())
return
# decision to include nums[i]
subset.append(nums[i])
dfs(i + 1)
# decision NOT to include nums[i]
subset.pop()
dfs(i + 1)
dfs(0)
return res