-
Notifications
You must be signed in to change notification settings - Fork 0
/
subsets.py
32 lines (28 loc) · 864 Bytes
/
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
class Solution(object):
def subsets(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
result = []
length = len(nums)
num = [''.join(str(i)) for i in nums]
num = ''.join(num)
possible = 2**length
for item in range(possible):
positions = bin(item)[2:]
if len(positions) != length:
diff = length - len(positions)
while diff:
positions = '0' + positions
diff -= 1
temp = []
print positions
for i in range(length):
if positions[i] == '1':
temp.append(nums[i])
result.append(temp)
return result
if __name__ == '__main__':
nums = [1, 2, 3]
print Solution().subsets(nums)