题目:

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. 反编译APK终结教程

    现在来教大家如何由网上下载的Android应用反编译为源码.如果你感兴趣,就来看一看吧.前提是你的电脑得已经配置好了java环境,如果没有配置好的话,下面我会附带一提,如果你还是不懂的话,那就上网搜一 ...

  2. 第25章 项目6:使用CGI进行远程编辑

    初次实现 25-1 simple_edit.cgi --简单的网页编辑器 #!D:\Program Files\python27\python.exeimport cgiform = cgi.Fiel ...

  3. java dom4j解析xml用到的几个方法

    1. 读取并解析XML文档: SAXReader reader = new SAXReader(); Document document = reader.read(new File(fileName ...

  4. python autopy

    今天学习了python autopy 主要包括alert,color,mouse,key,bitmap,screen等的操作 文档在http://www.autopy.org/documentatio ...

  5. nginx学习之一

    http://tengine.taobao.org/book/chapter_02.html

  6. 【python】 入门 搭建环境

    1.去官网下载包 基本程序编译器 python-2.7.10.msi 集成开发环境 pycharm-community-4.5.2.exe 包管理工具 pip-7.0.3.tar.gz 2.安装 按顺 ...

  7. Memcache+Tomcat9集群实现session共享(非jar式配置, 手动编写Memcache客户端)

    Windows上两个tomcat, 虚拟机中ip为192.168.0.30的centos上一个(测试用三台就够了, 为了测试看见端口所以没有使用nginx转发请求) 开始 1.windows上开启两个 ...

  8. 高效开发Android App的10个建议(转)

    假如要Google Play上做一个最失败的案例,那最好的秘诀就是界面奇慢无比.耗电.耗内存.接下来就会得到用户的消极评论,最后名声也就臭了.即使你的应用设计精良.创意无限也没用. 耗 电或者内存占用 ...

  9. SAP如何使用关于序列号的表

  10. DB天气app冲刺二阶段第五天

    昨天什么事情也没做..看了一场哆啦a梦 所以就不算冲刺了.. 今天主要就是做了一下需要用到的图片的整理还有的就是UI主界面需要展示用的素材,发现好多东西都需要搜索半天,虽然这个不是什么技术活..但真的 ...