subset leetcode-78

subset leetcode-78

Subject description:

Find all subsets of the array

Use python comes permutations function, note that the length of the sequence length i to be contained;

from itertools import combinations
class Solution:
    def subsets(self, nums: List[int]) -> List[List[int]]:
        res = []
        for i in range(len(nums)+1):
            res.extend(combinations(nums,i))
        res = [list(v) for v in res]
        return res
        

To realize his deep search

from itertools import combinations
class Solution:
    def subsets(self, nums: List[int]) -> List[List[int]]:
        def dfs(nums,index,path):
            res.append(path)
            for i in range(index,len(nums)):
                dfs(nums,i+1,path+[nums[i]])
        res = []
        nums.sort()
        dfs(nums,0,[])
        return res

Guess you like

Origin www.cnblogs.com/curtisxiao/p/11281005.html