-
Notifications
You must be signed in to change notification settings - Fork 24
/
78 - Subsets.py
56 lines (54 loc) · 1.8 KB
/
78 - Subsets.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Solution 1: Iterative O(n*(2^n))
class Solution(object):
def subsets(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
pset = []
size = 2 ** len(nums)
for i in range(size):
subset = []
for j in range(len(nums)):
if (2**j) & i == (2**j):
subset.append(nums[j])
pset.append(subset)
return pset
# Solution 2: Recursive O(n*(2^n))
class Solution(object):
def pset(self, nums, subsets):
if len(nums) == 0:
return subsets
newSubsets = []
for i in range(len(subsets)):
subset = subsets[i]
newSubsets.append(subset)
# HUGE gotcha on the next line
# You can't use subset.append(nums[0]) because this will mutate subset!!
# Instead use subset + [nums[0]] which gives a new list
newSubsets.append(subset + [nums[0]])
subsets = newSubsets
return self.pset(nums[1:],subsets)
def subsets(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
subsets = []
subsets.append([])
subsets.append([nums[0]])
return self.pset(nums[1:], subsets)
# Solution 3: I thought this solution was beautiful, takes advantage
# of list comprehension in Python.
# Runtime: O(n * (2^n)), it's hard to see with this concise code but keep in mind
# there are still 2^n subsets and it takes O(n) time to build each of them
class Solution(object):
def subsets(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
pset = [[]] # we'll always have the empty set
for num in nums:
pset += [subset + [num] for subset in pset]
return pset