LeetCode 90. Subsets II (子集合之二)
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 (子集合之二)的更多相关文章
- [LeetCode] 90. Subsets II 子集合之二
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- [LeetCode] Subsets II 子集合之二
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- [leetcode]90. Subsets II数组子集(有重)
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [LeetCode] 90.Subsets II tag: backtracking
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [LeetCode] 90. Subsets II 子集合 II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- leetCode 90.Subsets II(子集II) 解题思路和方法
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- Leetcode#90 Subsets II
原题地址 跟Subsets(参见这篇文章)类似. 但因为有重复元素,所以要考虑去重问题. 什么情况下会出现重复呢?比如S = {5, 5, 5},如果要选1个5,一共有C(3,1)=3种选法,即100 ...
- leetcode 78. Subsets 、90. Subsets II
第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...
- 【LeetCode】90. Subsets II (2 solutions)
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
随机推荐
- linux 编辑文件时 E45: 'readonly' option is set (add ! to override) 隐藏属性 chattr lsattr
在改一个系统当中的文件参数时, vim config.php 时,提示 E45: 'readonly' option is set (add ! to override) ,同时不能编辑不能删除不能设 ...
- MongoDB分片原理篇
MongoDB分片 为什么需要Sharded cluster? MongoDB目前3大核心优势:『灵活模式』+ 『高可用性』 + 『可扩展性』,通过json文档来实现灵活模式,通过复制集来保证高可用, ...
- [UIKit学习]06.懒加载,模型,自定义代码段,重写构造方法
懒加载 在get中加载,且只加载一次 - (NSArray *)shops { if (_shops == nil) { NSString *file = [[NSBundle mainBundle] ...
- 705. New Distinct Substrings spoj(后缀数组求所有不同子串)
705. New Distinct Substrings Problem code: SUBST1 Given a string, we need to find the total number o ...
- Python网络编程socket练习(TCP)
服务器端:server.py # -*- coding: utf-8 -*- from socket import * HOST='' PORT=5000 BUFF_SIZE=1024 ADDR=(H ...
- Java课堂作业01
题目:编写一个程序,此程序从命令行接收多个数字,求和之后输出结果. 设计思想:用for循环将string型转换为int型,再用sum求和,使其一直相加,到达最大长度,sum即为所求sum. 程序流程图 ...
- jQuery经典案例
示例1:鼠标点击左侧菜单实现打开和关闭功能: html及css代码部分: <!DOCTYPE html> <html lang="en"> <head ...
- Java历程-初学篇 Day07 循环结构2 for循环
一,格式 for(赋值语句//为循环变量赋初值;条件语句//循环结构的循环条件;赋值语句//迭代,修改循环变量的值){ //循环体; } 二,简单举例 for(int i=1;i<=10;i++ ...
- svn服务端安装、权限修改以及客户端的使用
2017-10-1016:10:2 svn服务端安装.权限修改以及客户端的使用 svn服务端.客户端.汉化包下载 http://pan.baidu.com/s/1c1Ogj2C 1.安装服务器端程序( ...
- JAVA提高一:静态导入、可变参数、增强型for循环、装拆箱
国庆假期已结束,假期8天,全部在家带娃,体会到了妻子的不容易,需要好好努力来多赚钱了,言归正传.10月份开始进去JAVA 高级语法知识学习,本节复习学习的为:静态导入.可变参数.增强型for循环.装拆 ...