# LeetCode OJ 系列之78 Subsets --Python

Problem:

Given a set of distinct integers, nums, return all possible subsets.

Note:

• Elements in a subset must be in non-descending order.
• 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],
[]
]
Answer:

class Solution(object):

def subsets(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
result = self.subset_func(nums)
for line in result:
line.sort()
return result

def subset_func(self,nums):
if len(nums)==0:
return [[]]
if len(nums)==1:
return [[],nums]
first = nums[0]
tmp_nums = nums[1:]
tmp_result = self.subset_func(tmp_nums)
result = []
for key in tmp_result:
result.append(key)
tmp = list(key)
tmp.append(first)
result.append(tmp)
return result


Analysis:

• 集合A里所有的项
• 集合A里每一项添加新的元素x之后形成的新项。

#### Leetcode 78. Subsets Python DFS 深度优先搜索解法

2018-03-15 18:37:00

#### LeetCode--Subsets（集合的子集）Python

2017-12-05 14:57:31

#### leetcode 78. Subsets-数组子集|回溯算法

2016-06-07 17:04:45

#### Python作业（LeetCode 78)

2018-04-23 23:01:46

#### 90. Subsets II Leetcode Python

2015-01-15 06:03:55

#### LeetCode 78. Subsets 解题报告

2016-02-17 09:09:55

#### leetcode practice - python3 (7)

2018-05-26 14:57:55

#### LeetCode78:Subsets

2015-07-06 17:33:29

#### leetcode practice - python3 (11)

2018-06-03 14:08:56

#### 【python】78.Subsets

2018-06-13 18:11:39