[Leetcode]@python 78. Subsets
题目链接
https://leetcode.com/problems/subsets/
题目原文
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],
[]
]
题目大意
其给定集合的所有子集
解题思路
dfs求解
代码
class Solution(object):
def subsets(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
def dfs(depth, start, valuelist):
ans.append(valuelist)
if depth == len(nums): return
for i in range(start, len(nums)):
dfs(depth + 1, i + 1, valuelist + [nums[i]])
nums.sort()
ans = []
dfs(0, 0, [])
return ans