-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubsets.py
More file actions
28 lines (24 loc) · 821 Bytes
/
subsets.py
File metadata and controls
28 lines (24 loc) · 821 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
class Solution:
def subsets(self, nums: 'List[int]') -> 'List[List[int]]':
list_len = len(nums)
if (list_len == 0):
return [[]]
res = [[[] for i in range(list_len+1)] for j in range(list_len+1)]
for i in range(list_len+1):
res[0][i]=[[]]
for i in range(1,list_len+1):
for j in range(i,list_len+1):
item = list()
up = res[i-1][j-1]
for it in up:
item.append(it + [nums[j-1]])
left = res[i][j-1]
for it in left:
item.append(it)
res[i][j] = item
ret = list()
for i in range(list_len+1):
ret += res[i][list_len]
return ret
s=Solution()
print(s.subsets([1,2,3]))