题目:

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

Note:

  • Elements in a subset must be in non-descending order.
  • 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],
[]
]

链接:  http://leetcode.com/problems/subsets-ii/

题解:

Subsets II, 关键是去重复。我们依然使用跟其他dfs +backtracking类似的方法, 当i > pos && nums[i] == nums[i - 1]的时候,跳过当前重复的元素。

Time Complexity - O(2n), Space Complexity - O(2n)。

public class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if(nums == null || nums.length == 0)
return res;
Arrays.sort(nums);
ArrayList<Integer> list = new ArrayList<>();
dfs(res, list, nums, 0);
return res;
} private void dfs(List<List<Integer>> res, ArrayList<Integer> list, int[] nums, int pos) {
res.add(new ArrayList<Integer>(list)); for(int i = pos; i < nums.length; i++) {
if(i > pos && nums[i] == nums[i - 1])
continue;
list.add(nums[i]);
dfs(res, list, nums, i + 1);
list.remove(list.size() - 1);
}
}
}

二刷:

跟Subsets I唯一不同的地方就是由重复。 和一刷一样,重点在于去重复。我们只需要在每一层遍历的时候,因为最开始sort过, 只要用i > pos && nums[i] == nums[i - 1]来跳过重复的元素就可以了。

Java:

Time Complexity - O(n!), Space Complexity (n2)

public class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums == null || nums.length == 0) {
return res;
}
Arrays.sort(nums);
List<Integer> list = new ArrayList<>();
subsetsWithDup(res, list, nums, 0);
return res;
} private void subsetsWithDup(List<List<Integer>> res, List<Integer> list, int[] nums, int pos) {
res.add(new ArrayList<Integer>(list));
for (int i = pos; i < nums.length; i++) {
if (i > pos && nums[i] == nums[i - 1]) {
continue;
}
list.add(nums[i]);
subsetsWithDup(res, list, nums, i + 1);
list.remove(list.size()- 1);
}
}
}

三刷:

时间复杂度和空间复杂度真的不可以糊弄...看评论里shenhualong的解释比较好,基本的递归分析。 还要继续好好训练思维。 去重复的方法也可以用在其他类似题目里。

这里T(n) =  T(n - 1) + T(n - 2) + ... + T(1),  因为 T(n - 1) = T(n - 2) + T(n - 3)... + T(1), 所以T(n) = 2 * T(n - 1),结果就是T(n) = 2n。 对于nums中的每一个数字,我们都要做一个Recursive call,所以这里用了O(n)的时间, 最终结果Time Complexity是 n * 2n。 空间复杂度的话因为我们每次要生成new ArrayList<>(),所以也是2n。

地里stellari大神的帖子分析得特别好,放在reference里了。

Java:

Time Complexity - O(n * 2n), Space Complexity (2n)

public class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums == null) return res;
Arrays.sort(nums);
getSubsetsWithDup(res, new ArrayList<Integer>(), nums, 0);
return res;
} private void getSubsetsWithDup(List<List<Integer>> res, List<Integer> list, int[] nums, int pos) {
res.add(new ArrayList<>(list));
for (int i = pos; i < nums.length; i++) {
if (i > pos && nums[i] == nums[i - 1]) continue;
list.add(nums[i]);
getSubsetsWithDup(res, list, nums, i + 1);
list.remove(list.size() - 1);
}
}
}

Reference:

http://blog.csdn.net/linhuanmars/article/details/24286377

https://leetcode.com/discuss/46668/recursive-iterative-manipulation-solutions-explanations

http://www.1point3acres.com/bbs/thread-117602-1-1.html

90. Subsets II的更多相关文章

  1. leetcode 78. Subsets 、90. Subsets II

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

  2. 78. Subsets(M) & 90. Subsets II(M) & 131. Palindrome Partitioning

    78. Subsets Given a set of distinct integers, nums, return all possible subsets. Note: The solution ...

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

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

  4. 【LeetCode】90.Subsets II

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

  5. LeetCode Problem 90. Subsets II

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

  6. 78. Subsets 90. Subsets II

    1. Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset m ...

  7. 90. Subsets II (Back-Track, DP)

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

  8. Leetcode#90 Subsets II

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

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

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

随机推荐

  1. for xml path以及sql合并查询

    sql中for xml path的用法. http://www.cnblogs.com/yanghaibo/archive/2010/06/04/1751405.html

  2. ros-Qt代码环境的搭建

    1 建立package catkin_create_pkg beginner_tutorials roscpp 2 导入Qt Qt中打开整个工作空间的src/CMakeLists.txt 在倒数第二行 ...

  3. openerp模块收藏 基于Lodop的报表打印模块(转载)

    基于Lodop的报表打印模块 原文:http://shine-it.net/index.php/topic,7397.0.html 前段时间写了个小模块,来解决OE中报表打印不方便的问题.借鉴了 @b ...

  4. 命令行参数的处理函数getopt

    命令参数 在linux下, shell命令的参数分两种情况: a.参数需要附加信息, 如"wget http://www.abc.com/1.zip -o 1.zip" b.参数不 ...

  5. 序列化form表单内容为json对象

    SourceCode: ; (function ($) { $.fn.extend({ serializeJson: function () { var json = {}; $(this.seria ...

  6. .NET4.5中WCF中默认生成的basicHttpsBinding的研究

    起因: 使用.net4.5建立了一个空白的WCF服务.默认使用的绑定配置是basicHttpsBinding. 问题发现: 1.用客户端进行服务引用,生成了默认的配置文件,其中绑定配置是basicHt ...

  7. Entity Framework学习笔记(六)----使用Lambda查询Entity Framework(1)

    请注明转载地址:http://www.cnblogs.com/arhat 在前几章中,老魏一直使用Linq来查询Entity Framework.但是老魏感觉,如果使用Linq的话,那么Linq的返回 ...

  8. Sqlyog增加试用期

    win+r->输入regedit->进入注册表 在计算机->HKEY_CURRENT_USER->Software->{906D6D9F-AB51-429F……}中删除I ...

  9. 开源 P2P 直播 视频会议

    转自:http://blog.csdn.net/pkueecser/article/details/8223074 一个P2P点播直播开源项目:P2PCenter(我转过来的时候发现已经都打不开了.. ...

  10. 【BZOJ】【3143】【HNOI2013】游走

    数学期望/高斯消元/贪心 啊……用贪心的思路明显是要把经过次数期望越大的边的权值定的越小,那么接下来的任务就是求每条边的期望经过次数. 拆边为点?nonono,连接x,y两点的边的期望经过次数明显是 ...