26-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:

Input: nums = [1,2,3]
Output:
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

代码实现:

 1 class Solution(object):
 2     def subsets(self, nums):
 3         """
 4         :type nums: List[int]
 5         :rtype: List[List[int]]
 6         """
 7         res = [[]]
 8         for num in sorted(nums):
 9             res += [item+[num] for item in res]
10         return res

 

posted @ 2019-06-16 21:57  mingyu02  阅读(105)  评论(0编辑  收藏  举报