-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsub.py
More file actions
29 lines (25 loc) · 1.01 KB
/
sub.py
File metadata and controls
29 lines (25 loc) · 1.01 KB
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
class Solution(object):
def subsets(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
# Using itertools combinations - Short and easy logic - 100 pass
import itertools
result = []
for i in range(len(nums)+1):
for comb in itertools.combinations(nums,i): # use permutation when order is important as in (2,1) != (1,2)
result.append(comb)
return result
"""
# Learn about BackTracking technique
# Recursion method without itertools --> Just to make sure its implementable in other languages - 100 pass
def combinations(ans, nums):
for i,v in enumerate(nums):
value = ans + [v]
result.append(value)
combinations(value, nums[i+1:])
result = [[]] # Adding the empty set as the first entry without any elements
combinations([], nums)
return result
"""