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],
[]
]
这个题目思路就是类似于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. 
import copy
class Solution:
def subsets(self, nums: 'List [int]') -> 'List [ List [int] ]' :
ans = []
def helper(nums, ans, temp, pos):
ans.append(copy.deepcopy(temp))
for i in range(pos, len(nums)):
if i > pos and nums[i] == nums[i - 1]:
continue
temp.append(nums[i])
helper(nums, ans, temp, i + 1)
temp.pop()
nums.sort()
helper(nums, ans, [], 0)
return ans
2) Use the same idea , but get rid of deepcopy
 
class Solution:
def subsets(self, nums: 'List [int]') -> 'List [ List [int] ]' :
ans = []
def helper(nums, ans, temp, pos):
ans.append(temp)
for i in range(pos, len(nums)):
if i > pos and nums[i] == nums[i - 1]:
continue
helper(nums, ans, temp + [nums[i]], i + 1)
nums.sort() # Optional
helper(nums, ans, [], 0)
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. Nginx SSL 结合Tomcat 重定向URL变成HTTP的问题

    http://www.siven.net/posts/d925bb5d.html *********************************************** 问题描述 由于要配置服 ...

  2. 从零开始搭建FAQ引擎--深度语义匹配

    从零开始搭建FAQ引擎--深度语义匹配

  3. [Linux]systemd和sysV

    转自:https://www.cnblogs.com/EasonJim/p/7168216.html 在Debian8中systemd和sysVinit同时存在,NTP就是在/etc/init.d/n ...

  4. 苹果应用商店逾千款iOS应用存安全漏洞

    据国外网站Ibtimes报道,知名网络安全公司FireEye日前警告称,由于一款名为“JSPatch”.可帮助开发者修改应用程序的软件上存在安全漏洞,导致苹果应用商店内1000多款使用了该框架的iOS ...

  5. acl && prefix list

    acl number 2001                                         设置acl的编号rule 0 permit source 1.1.1.0 0.0.0.2 ...

  6. java把一个list分割成多个list存入map中(实例)

    这都是最近我写工具遇到的一些点, 这些点就是指我在网上没搜到答案,然后实际上我为此花费了时间的 public static void main(String[] args) { List<Str ...

  7. c++中为什么可以通过指针或引用实现多态,而不可以通过对象呢?

    引言:  在c++中司空见惯的事情就是:可以通过指针和引用可以实现多态,而对象不可以.  那为什么?让我们来解开这神秘的暗纱! 1. 类对象的存储方式: 在一个类的实例中,只会存放非静态的成员变量. ...

  8. wampserver2.2e-php5.3.13 版本 增加 php7 支持

    公司需要升级PHP版本至PHP7,对于用惯了wamp的来说,添加一下PHP扩展应该是很容易的,看我以前的文章(WampServer自己DIY添加apache.php.mysql版本). 不过再配置的当 ...

  9. nginx的https和http共存反向代理配置

    一.设置http反向代理: upstream ly.com { server ; server ; } upstream home.ly.com { server ; server ; } 对应增加: ...

  10. 学习Vue 入门到实战——学习笔记

    闲聊: 自从进了现在的公司,小颖就再没怎么接触vue了,最近不太忙,所以想再学习下vue,就看了看vue相关视频,顺便做个笔记嘻嘻. 视频地址:Vue 入门到实战1.Vue 入门到实战2 学习内容: ...