给定一个整数数组nums,里面的数值皆不相同,要求返回所有可能的子集。
使用DFS算法,具体思路如下图所示:
注意:递归截止的条件为startIndex == len(nums)
。
class Solution(object):
def subsets(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
def helper(startIndex, path):
res.append(path)
if startIndex == len(nums):
return
for i in range(startIndex, len(nums)):
helper(i+1, path+[nums[i]])
if not nums:
return []
res = []
helper(0, [])
return res