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

[LC] 90. Subsets II

时间:2019-10-21 09:28:45      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:OWIN   char   collect   sam   eve   possible   style   input   while   

Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: [1,2,2]
Output:
[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]

class Solution(object):
    def subsetsWithDup(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        res, lst = [], []
        if nums is None or len(nums) == 0:
            return res
        nums.sort()
        self.dfs(nums, 0, lst, res)
        return res
    
    def dfs(self, nums, level, lst, res):
        if level == len(nums):
            res.append(list(lst))
            return
        lst.append(nums[level])
        self.dfs(nums, level + 1, lst, res)
        lst.pop()

        # after adding the char, skip the following same one
        while level < len(nums) - 1 and nums[level] == nums[level + 1]:
            level += 1
        self.dfs(nums, level + 1, lst, res)
        

 

[LC] 90. Subsets II

标签:OWIN   char   collect   sam   eve   possible   style   input   while   

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

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