leetcode-78-子集

题目描述:

leetcode-78-子集_第1张图片

方法一:回溯

class Solution:
    def subsets(self, nums: List[int]) -> List[List[int]]:
        if not sum:
            return []
        res = []
        n = len(nums)
        def backtrack(idx,temp_list):
            res.append(temp_list)
            for i in range(idx,n):
                backtrack(i+1,temp_list+[nums[i]])
        backtrack(0,[])
        return res

方法二:迭代

class Solution: 
    def subsets(self, nums: List[int]) -> List[List[int]]: 
        res = [[]] 
        for i in nums: 
            res = res + [[i] + num for num in res] 
        return res

 

转载于:https://www.cnblogs.com/oldby/p/11171471.html

你可能感兴趣的:(leetcode-78-子集)