Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
这道题的题意很简单,就是求出一个集合的所有子集合,并且不重复。
这个题有多种解法
我先提供Bit Manipulation这种解法的Python代码。
class Solution:
def subsets(self, nums):
maxNum = 2**len(nums) - 1
res = []
for i in range(maxNum + 1):
res.append([])
j = 0
while i:
if i & 1:
res[-1].append(nums[j])
i >>= 1
j += 1
return res