[LC] 78. Subsets

Posted xuanlu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LC] 78. Subsets相关的知识,希望对你有一定的参考价值。

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: nums = [1,2,3]
Output:
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

Time: O(2^n)
Space: O(N)
class Solution:
    def subsets(self, nums: List[int]) -> List[List[int]]:
        res = []
        if nums is None or len(nums) == 0:
            return res
        self.helper(nums, 0, [], res)
        return res
            
    def helper(self, nums, index, combination, combinations):
        combinations.append(list(combination))   
        for i ian range(index, len(nums)):
            combination.append(nums[i])
            self.helper(nums, i + 1, combination, combinations)
            combination.pop()
    
    
    

 

以上是关于[LC] 78. Subsets的主要内容,如果未能解决你的问题,请参考以下文章

78. Subsets

[LC] 90. Subsets II

78. Subsets

78. Subsets

78. Subsets

78. Subsets