90. Subsets II
题目:
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],
[]
]
题解:
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的更多相关文章
- leetcode 78. Subsets 、90. Subsets II
第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...
- 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 ...
- 【LeetCode】90. Subsets II (2 solutions)
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 【LeetCode】90.Subsets II
Subsets II Given a collection of integers that might contain duplicates, nums, return all possible s ...
- LeetCode Problem 90. Subsets II
python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(sel ...
- 78. Subsets 90. Subsets II
1. Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset m ...
- 90. Subsets II (Back-Track, DP)
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 90. Subsets II (子集合之二)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
随机推荐
- Oracle 动态视图5 V$SESSION_LONGOPS
一.视图V$SESSION_LONGOPS显示运行超过6秒的操作的状态.包括备份,恢复,统计信息收集,查询等等 Column Datatype Description SID NUMBER Sessi ...
- Python计算斗牛游戏的概率
Python计算斗牛游戏的概率 过年回家,都会约上亲朋好友聚聚会,会上经常会打麻将,斗地主,斗牛.在这些游戏中,斗牛是最受欢迎的,因为可以很多人一起玩,而且没有技术含量,都是看运气(专业术语是概率). ...
- 微软更换考试中心后报名攻略以及MCT半价
微软从Prometric更换为Pearson的考试中心,比起以前的预约流程更加便捷. Pearson VUE为微软公司提供MCP考试服务 Pearson VUE为微软公司提供MCP考试服务 http: ...
- Microsoft Virtual Academy 介绍
Microsoft Virtual Academy 是微软的虚拟学院,会推出微软各个方面的一些教程 介绍一点有用的链接 http://www.microsoftvirtualacademy.com/e ...
- ORACLE 11G 配置DG 报ORA-10458、ORA-01152、ORA-01110
操作系统: Oracle Linux Server release 5.7 数据库版本: Oracle Database 11g Enterprise Edition Release 11.2.0.3 ...
- MYSQL创建多张表,相同表结构,不同表名
#!/bin/bashfor i in {0..63}domysql -u$1 -p$2 -h127.0.0.1 <<EOFuse yoon;create table ivc_pre_de ...
- MVC中System.InvalidOperationException: 传入字典的模型项的类型为“XXX”,但此字典需要类型“XXA”的模型项
出现此类错误的一个原因是Controller传过去的Model和View中的Model不是同一个Model
- C#获取数据库中的Instance
如果我现在要写个代码生成器,连接数据库,那你得知道有哪些Database存在吧,不然咋整? 在VS中我们添加一个ADO.NET的实体模型 在选择数据库名称的时候就是获取了数据库中Database In ...
- 银行HR:寒门再难出贵子
银行HR:寒门再难出贵子来源:金融行业网 2013 年 8 月 6 日 来源:天涯社区 作者:永乐大帝二世 本文是一位银行的HR写的,他工作了10年,接待了一群到银行实习的实习生,然后观察他们发生的好 ...
- thinkphp中curl的使用,常用于接口
/lib/action/PublicAction.class.php class PublicAction extends Action{ //curl,返回数组 public function ge ...