class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
if not nums: return [[]]
res=[]
for p in self.subsets(nums[1:]):
res.append(p) # 子集忘了加了,单单把父集合加了
# print(nums[:1]+p)
res.append(nums[:1]+p)
return res
使用位运算,对每一位为1的表示这一位的数字存在
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
t=len(nums)
res=[]
for i in range(2**t):
tmp=[]
for j in range(t):
if i&(1<<j):tmp.append(nums[t-1-j])
res.append(tmp)
return res
网友评论