-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path98.py
26 lines (23 loc) · 760 Bytes
/
98.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
# Runtime: 40 ms, faster than 98.77% of Python3 online submissions for Subsets.
# Difficulty: Medium
class Solution:
def memoize(fn):
cache = dict()
def memoized_fn(self, nums):
if tuple(nums) in cache:
return cache[tuple(nums)]
cache[tuple(nums)] = fn(self, nums)
return cache[tuple(nums)]
return memoized_fn
@memoize
def subsets(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if not nums:
return [[]]
subsets = list()
subsets.extend(self.subsets(nums[1:]))
subsets.extend(map(lambda subset: [nums[0]] + subset, self.subsets(nums[1:])))
return subsets