-
Notifications
You must be signed in to change notification settings - Fork 0
/
78. Subsets.py
30 lines (29 loc) · 876 Bytes
/
78. 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
# class Solution:
# def subsets(self, nums: [int]) -> [[int]]:
# res = []
#
# def backtrack(cur, path):
# if path:
# res.append(path[:])
# if cur == len(nums) - 1:
# return
# for i in range(cur + 1, len(nums)):
# path.append(nums[i])
# backtrack(i, path)
# path.pop(-1)
#
# backtrack(-1, [])
# res.append([])
# return res
class Solution:
def subsets(self, nums: [int]) -> [[int]]:
bitmask = 1 << (len(nums) + 1)
res = []
for i in range(bitmask >> 1, bitmask):
temp = []
bit_str = bin(i)[2:]
for i in range(len(bit_str)):
if bit_str[i] == "1":
temp.append(nums[i])
res.append(temp[:])
return res