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

Note: The solution set must not contain duplicate subsets.

For example,
If nums = [1,2,2], a solution is:

[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]

题目标签:Array

  题外话:搬完家后终于又回来刷题了,这次搬家整整用了2个星期,毕竟是从东北直接搬来了中南,光是开车就开了4天,每天开7小时,一共28小时。。。可是够累的。再加上各种杂事,看房租房,买家具,安装网络,换车牌。总算是差不多全搞定了,为了找一份工作也是不容易,那么回到刷题。

  这道题目和78. subset 方法二基本一样,只是多加了一个条件来防止duplicates subsets。那么如何防止duplicates subsets呢,我们来看原题例子 [1,2,2], 利用78. subset 的方法二, 我们可以得到 [ [ ], [1], [1,2], [1,2,2], [1,2], [2], [2,2], [2] ],其中有2个重复的子集合,[1,2] 和 [2]。根据方法二的code,我们可以发现,第二个 [1,2] 是在 for(int i=1; i<nums.length; i++)  这里面产生的。当 [1, 2, 2] 结束之后,remove 最后一个数字 变成 [1, 2],然后这个[1, 2]的 for loop 也结束了,返回后删除最后一个数字,变为 [1], 接着 for loop继续,移动到第二个2, 组成 [1,2] 产生的 重复子集合。 同样的,第二个 [2] 是在 for(int i=0; i<nums.length; i++) 这里面产生的, 当 [2, 2] 结束之后,删除最后一个数字变为[2],返回之后又删除最后一个数字变为 [ ], 然后for loop 继续移动到第二个2, 变为 [2] 产生的重复子集合。

  所以,重复的子集合都是在for loop里产生的,而且都是在第二个重复的数字那里。所以只需要多加一个条件 - 当遇到第二个重复的数字,直接跳过, 这样就可以在for loop 里避免重复的子集合。具体看code。

Java Solution:

Runtime beats 63.22%

完成日期:08/25/2017

关键词:Array

关键点:递归, 跳过第二个重复的数字来避免重复子集合

 public class Solution
{
public List<List<Integer>> subsetsWithDup(int[] nums)
{
// sort nums array
Arrays.sort(nums);
// create res
List<List<Integer>> res = new ArrayList<>();
// call recursion function
helper(res, new ArrayList<>(), 0, nums); return res;
} public static void helper(List<List<Integer>> res, List<Integer> tmpRes, int pos, int[] nums)
{
// here should be <= not just < because we need to add the new tmpRes in next recursion.
// Therefore, we need one more bound to add tmpRes
if(pos <= nums.length)
res.add(new ArrayList<>(tmpRes)); // once the new recursion is finished, remove the last number in the tmpRes and continue to
// add rest numbers to get new subsets
for(int i=pos; i<nums.length; i++)
{
if(i > pos && nums[i] == nums[i-1]) // avoid duplicates
continue; tmpRes.add(nums[i]);
helper(res, tmpRes, i+1, nums);
tmpRes.remove(tmpRes.size()-1);
}
}
}

参考资料:

https://discuss.leetcode.com/topic/22638/very-simple-and-fast-java-solution/4

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 90. Subsets II (子集合之二)的更多相关文章

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

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

  2. [LeetCode] Subsets II 子集合之二

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

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

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

  4. [LeetCode] 90.Subsets II tag: backtracking

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

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

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

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

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

  7. Leetcode#90 Subsets II

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

  8. leetcode 78. Subsets 、90. Subsets II

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

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

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

随机推荐

  1. python基础之元组,集合

    一.元组 为何要有元组,存放多个值,元组不可变,更多的是用来做查询 t=(,[,],,)) #t=tuple((,[,],,))) print(type(t)) 元组可以作为字典的key d={(,, ...

  2. [06] Session实现机制以及和Cookie的区别

    1.为什么有Session和Cookie 根据早期的HTTP协议,每次request-reponse时,都要重新建立TCP连接.TCP连接每次都重新建立,所以服务器无法知道上次请求和本次请求是否来自于 ...

  3. spring 注解方式配置Bean

    Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件特定组件包括: @Component:基本注解,标示了一个受Spring管理的Bean组件 @Respository:标识 ...

  4. Linux SSH下安装Java并设置环境

    我是用Xshell进行远程连接阿里云服务器的,所以jdk不好下载. 我使用的是Winscp远程软件,在window上下载了jdk然后再上传到Linux服务器上 下面是安装的步骤 1.下载jdk8 登录 ...

  5. Spring 使用AspectJ的三种方式

    Spring 使用AspectJ 的三种方式 一,使用JavaConfig 二,使用注解隐式配置 三,使用XML 配置 背景知识: 注意 使用AspectJ 的 时候 要导入相应的Jar 包 嗯 昨天 ...

  6. [python学习笔记] 开篇

    今天开始学习markdown,立贴为证. 平均一天一小时.两个月先学完基础. 所有的帖子只为了学习记录,有不对的请提出,轻吐槽

  7. OC——关于KVC

    我们知道在C#中可以通过反射读写一个对象的属性,有时候这种方式特别方便,因为你可以利用字符串的方式去动态控制一个对象.其实由于ObjC的语言特性,你根部不必进行任何操作就可以进行属性的动态读写,这种方 ...

  8. Linux学习——shell编程之变量

    shell编程之变量:Linux shell编程基础中的变量. 包括Bash变量的分类和各变量的详细使用,如:用户自定义变量.环境变量.语系变量.位置参数变量和预定义变量. 1:什么是Bash变量? ...

  9. VC++:创建,调用Win32动态链接库

    VC++:创建,调用Win32动态链接库 概述 DLL(Dynamic Linkable Library)动态链接库,Dll可以看作一种仓库,仓库中包含了可以直接使用的变量,函数或类.仓库的发展史经历 ...

  10. 深入浅出AQS之条件队列

    相比于独占锁跟共享锁,AbstractQueuedSynchronizer中的条件队列可能被关注的并不是很多,但它在阻塞队列的实现里起着至关重要的作用,同时如果想全面了解AQS,条件队列也是必须要学习 ...