码迷,mamicode.com
首页 > 其他好文 > 详细

[LC] 78. Subsets

时间:2019-09-22 10:40:08      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:res   class   def   note   dup   NPU   ati   nbsp   for   

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

标签:res   class   def   note   dup   NPU   ati   nbsp   for   

原文地址:https://www.cnblogs.com/xuanlu/p/11566071.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!