題目描述
給定一組不含重復(fù)元素的整數(shù)數(shù)組 nums,返回該數(shù)組所有可能的子集(冪集)塌西。
說(shuō)明:解集不能包含重復(fù)的子集他挎。
示例:
輸入: nums = [1,2,3]
輸出:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
思路解析
回溯
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
if not nums:
return []
n = len(nums)
res = []
def helper(i, temp):
res.append(temp)
for j in range(i, n):
helper(j + 1, temp + [nums[j]])
helper(0, [])
return res
Ac78