-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsets.py
More file actions
36 lines (32 loc) · 898 Bytes
/
Copy pathSubsets.py
File metadata and controls
36 lines (32 loc) · 898 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
32
33
34
35
36
__author__ = 'Martin'
class Solution(object):
def subsets1(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
nums = sorted(nums)
return [[]] + self.bt(nums)
def bt(self, nums):
if len(nums) == 0:
return []
if len(nums) == 1:
return [nums]
res = []
for i in range(len(nums)):
res.append([nums[i]])
for j in self.bt(nums[i+1:]):
res.append([nums[i]] + j)
return res
def subsets(self, S):
def dfs(depth, start, valuelist):
res.append(valuelist)
if depth == len(S): return
for i in range(start, len(S)):
dfs(depth+1, i+1, valuelist+[S[i]])
S.sort()
res = []
dfs(0, 0, [])
return res
s = Solution()
print(s.subsets([1,2,3,4]))