---
title: "#78 Subsets"
tags: LeetCode, Top100
---
#78 Subsets
==
題目描述
--
Given a set of **distinct** integers, nums, return all possible subsets (the power set).
**Note**: The solution set must not contain duplicate subsets.
Example 1:
--
>Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
解題思維
--
利用bit的特性,找出不重複的子集合。
Using bit
--
```python=
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
res = []
n = len(nums)
bits = bin(2 ** n-1)[2:]
pointer = 0
while bin(pointer)[2:] != bits:
cur = bin(pointer)[2:].rjust(n, '0')
thisSolution = []
for i, c in enumerate(cur[::-1]):
if c == '0':
continue
thisSolution.append(nums[i])
res.append(thisSolution)
pointer += 1
res.append(nums)
return res
```