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. ionic 打包安卓包

    一.配置环境: 先按照之前的文章,配置好环境需要: 二.安装 1. 这里前提是 需要安装  node (地址: http://nodejs.cn/download/) 命令: node -v   // ...

  2. webservice第二篇【自定义webservice服务、soa、uddi概念、soap协议】

    自定义webservice服务 我们在上一章节中已经使用wsimport生成本地代理来调用webservice的服务了,其实我们自己写的web应用程序也是可以发布webservice的 我们发布了we ...

  3. Web开发模式【Mode I 和Mode II的介绍、应用案例】

    开发模式的介绍 在Web开发模式中,有两个主要的开发结构,称为模式一(Mode I)和模式二(Mode II) 首先我们来理清一些概念吧: DAO(Data Access Object):主要对数据的 ...

  4. java基础知识5--集合类(Set,List,Map)和迭代器Iterator的使用

    写的非常棒的一篇总结: http://blog.csdn.net/speedme/article/details/22398395#t1 下面主要看各个集合如何使用迭代器Iterator获取元素: 1 ...

  5. 反转字符串的几种实现(Java)

    反转字符串的几种实现(Java) 首先第一种是利用Java中的类库对象进行反转 //第一种 使用Java类库的diam实现反转 public String reverse(String str){ S ...

  6. vue组件初学--弹射小球

    1. 定义每个弹射的小球组件( ocicle ) 2. 组件message自定义属性存放小球初始信息(可修改) { top: "0px", //小球距离上方坐标 left: &qu ...

  7. Hadoop 2:Mapper和Reduce

    Hadoop 2:Mapper和Reduce Understanding and Practicing Hadoop Mapper and Reduce 1 Mapper过程 Hadoop将输入数据划 ...

  8. Egg + Vue 服务端渲染工程化实现

    在实现 egg + vue 服务端渲染工程化实现之前,我们先来看看前面两篇关于Webpack构建和Egg的文章: 在 Webpack工程化解决方案easywebpack 文章中我们提到了基于 Vue ...

  9. bzoj4557【JLOI2016】侦查守卫

    这道题对于我来说并不是特别简单,还可以. 更新一下blog 树形DP f[i][j]表示i的子树中,最高覆盖到i向下第j层的最小花费. g[i][j]表示i的子树全部覆盖,还能向上覆盖j层的最小花费. ...

  10. nginx配置文件作用介绍

    ######Nginx配置文件nginx.conf中文详解##### #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_ ...