# Leetcode 78：子集（最详细的解法！！！）

**说明：**解集不能包含重复的子集。

输入: nums = [1,2,3]

[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]


class Solution:
def subsets(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if not nums:
return [[]]
result = self.subsets(nums[1:])
return result + [[nums[0]] + s for s in result]


class Solution:
def subsets(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
result = list()
self._subsets(nums, 0, list(), result)
return result

def _subsets(self, nums, index, path, result):
result.append(path)
for i in range(index, len(nums)):
self._subsets(nums, i + 1, path + [nums[i]], result)


self._subsets(nums, i + 1, path + [nums[i]], result)


path.append(nums[i])
self._subsets(nums, i + 1, path, result)
path.pop()


class Solution:
def subsets(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
result = [[]]
for num in nums:
result += [item+[num] for item in result]
return result


class Solution:
def subsets(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
return [[nums[j] for j in range(len(nums)) if i>>j&1] for i in range(2**len(nums))]


00000000
00000001
00000010
00000011
00000100
00000101
00000110
00000111