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:

  1. Input: [1,2,2]
  2. Output:
  3. [
  4. [2],
  5. [1],
  6. [1,2,2],
  7. [2,2],
  8. [1,2],
  9. []
  10. ]
这个题目思路就是类似于DFS的backtracking. 是[LeetCode] 78. Subsets tag: backtracking 的update版本,或者说更加general的,因为允许有重复了。
所以在Subsets的基础/模板上,先sort nums,再加个判断,在for loop循环中,如果当前的跟前一个元素相同就跳过,相当于每次只取一次同样的数(在同一个for loop中)。
 
1. Constraints
1) edge case len(nums) == 0 => [ [ ] ]  但是还是可以
 
2. Ideas
DFS 的backtracking    T: O(2^n)     S; O(n)
 
3. Code 
 
1) using deepcopy, but the idea is obvious, use DFS [] -> [1] -> [1, 2] -> [1, 2, 3] then pop the last one, then keep trying until the end. 
  1. import copy
  2. class Solution:
  3. def subsets(self, nums: 'List [int]') -> 'List [ List [int] ]' :
  4. ans = []
  5. def helper(nums, ans, temp, pos):
  6. ans.append(copy.deepcopy(temp))
  7. for i in range(pos, len(nums)):
  8. if i > pos and nums[i] == nums[i - 1]:
  9. continue
  10. temp.append(nums[i])
  11. helper(nums, ans, temp, i + 1)
  12. temp.pop()
  13. nums.sort()
  14. helper(nums, ans, [], 0)
  15. return ans
2) Use the same idea , but get rid of deepcopy
 
  1. class Solution:
  2. def subsets(self, nums: 'List [int]') -> 'List [ List [int] ]' :
  3. ans = []
  4. def helper(nums, ans, temp, pos):
  5. ans.append(temp)
  6. for i in range(pos, len(nums)):
  7. if i > pos and nums[i] == nums[i - 1]:
  8. continue
  9. helper(nums, ans, temp + [nums[i]], i + 1)
  10. nums.sort() # Optional
  11. helper(nums, ans, [], 0)
  12. return ans

[LeetCode] 90.Subsets II tag: backtracking的更多相关文章

  1. [leetcode]90. Subsets II数组子集(有重)

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

  2. [LeetCode] 90. Subsets II 子集合 II

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

  3. LeetCode 90. Subsets II (子集合之二)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  4. [LeetCode] 90. Subsets II 子集合之二

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  5. Leetcode#90 Subsets II

    原题地址 跟Subsets(参见这篇文章)类似. 但因为有重复元素,所以要考虑去重问题. 什么情况下会出现重复呢?比如S = {5, 5, 5},如果要选1个5,一共有C(3,1)=3种选法,即100 ...

  6. leetCode 90.Subsets II(子集II) 解题思路和方法

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  7. leetcode 78. Subsets 、90. Subsets II

    第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...

  8. 【LeetCode】90. Subsets II (2 solutions)

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  9. LeetCode Problem 90. Subsets II

    python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(sel ...

随机推荐

  1. JQuery DataTables Selected Row

    获取单行选中的值 $('#MonitoringTypeTable tbody').on('click', 'tr', function () { if ($(this).hasClass('selec ...

  2. DTO转DOMAIN动态转换类。

    package dtotransfer.util; import dtotransfer.annotation.DomainField; import java.lang.annotation.Ann ...

  3. 解决IE8下opacity属性失效问题,无法隐藏元素

    解决IE8下opacity属性失效问题   由于opacity属性存在兼容性问题,所以在IE8下,用opacity来设置元素的透明度,会失效,从而导致页面的样式问题.在IE8及其更早的浏览器下,我们可 ...

  4. 最简单的设计模式——单例模式的演进和推荐写法(Java 版)

    前言 如下是之前总结的 C++ 版的:软件开发常用设计模式—单例模式总结(c++版),对比发现 Java 实现的单例模式和 C++ 的在线程安全上还是有些区别的. 概念不多说,没意思,我自己总结就是: ...

  5. [微信小程序] 通过快速启动demo分析小程序入门关键点

    (1)小程序基础结构 下图是在开发者工具通过快速启动模式创建的小程序的目录结构 可以看到,小程序中主要包含有4中类型不同的文件 .json 后缀的 JSON 配置文件 .wxml 后缀的 WXML 模 ...

  6. thinkphp3.2在nginx下的配置

    最近一直没用nginx 昨天将tp3.2的项目部署到Ubuntu下的nginx下,发现忘记怎么配置的了 特将配置方式记录下来,以方便日后查找 服务器nignx 配置文件 server { listen ...

  7. 安装Conda并在Conda下安装jupyter notebook

    1:安装 conda install jupyter notebook 2:启动 jupyter notebook

  8. bootstrap引入文件方法

    <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8& ...

  9. 11.8Django中的组件content_type

    2018-11-8 18:59:11 在Django中已经有一个contenttype这个组件,并且在python manage.py makemigrations 和migrate的时候,一起在数据 ...

  10. Java课程寒假之《人月神话》有感之一

    一.焦油坑 以前上课的时候,老师讲过早期的程序由于工作量不大,大多只需要几个人完成,随着软件规模的不断扩大,代码量直线上升,仅仅一两个人可能没有办法完成这样的任务,多以开始形成了团队的规模,焦油坑说的 ...