Given a set of distinct integers, nums, return all possible subsets.
Note: The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,3], a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
题目的意思很简单,就是给我们一个集合,求出他的所有的子集,包括空集:
class Solution(object):
def subsets(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res = set([()])#先建立一个空集,这个空集里包含了空子集
for num in sorted(nums):
res.update([item+(num, ) for item in res if item+(num,) not in res])#这里需要注意的是需要判定是否已经包含在内
return [list(item) for item in res]#生成列表