-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path78-Subsets.py
More file actions
31 lines (24 loc) · 703 Bytes
/
Copy path78-Subsets.py
File metadata and controls
31 lines (24 loc) · 703 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
27
28
29
30
31
'''Leetcode- https://leetcode.com/problems/subsets/'''
'''
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.
Input: nums = [1,2,3]
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
'''
def subsets(nums):
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
#T :O(2^n)
#S :O(n)