Subsets II 解答
Question
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],
[]
]
Solution
Similar with Subset I. Here, we need to consider how to deal with duplicates.
public class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> result = new ArrayList<List<Integer>>();
List<Integer> record = new ArrayList<Integer>();
dfs(nums, 0, record, result);
return result;
}
private void dfs(int[] nums, int start, List<Integer> list, List<List<Integer>> result) {
if (start <= nums.length)
result.add(new ArrayList<Integer>(list));
if (start == nums.length)
return;
int prev = nums[start];
for (int i = start; i < nums.length; i++) {
if (i > start && nums[i] == prev)
continue;
list.add(nums[i]);
dfs(nums, i + 1, list, result);
list.remove(list.size() - 1);
prev = nums[i];
}
}
}
Conclusion -- Two ways to deal with duplicates in result
如这一题,输入数组中有重复数字,所以如果不考虑处理重复,结果中也会有重复数字。
有两种处理重复的方法。
1. 加入result前,判断该子结果是否已经存在。
2. Skip 方法
方法一会造成大量时间空间浪费,所以不建议。下面重点总结方法二。
以该题为例,我们画出它的解空间树。


我们发现其实重复的部分就是相当于重复的子树,因此直接跳过重复的数字即可。
Subsets II 解答的更多相关文章
- 42. Subsets && Subsets II
Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...
- 【leetcode】Subsets II
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 90. Subsets II
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 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 ...
- Subsets II - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Subsets II - LeetCode 注意点 有重复的数字 数组可能是无序的,要先排序 解法 解法一:递归,只需要在Subsets中递归写法的基础上 ...
- 【LeetCode】90. Subsets II (2 solutions)
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- leetcode 78. Subsets 、90. Subsets II
第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...
- LeetCode解题报告—— Word Search & Subsets II & Decode Ways
1. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be con ...
- [Leetcode Week8]Subsets II
Subsets II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/subsets-ii/description/ Description Given ...
随机推荐
- 前端HTML与CSS编码规范
HTML 语法 HTML5 doctype 语言属性(Language attribute) 字符编码 IE 兼容模式 引入 CSS 和 JavaScript 文件 实用为王 属性顺序 布尔(bool ...
- Display number of replies in disscussion board
how to display number of replies in disscussion board I have a require about display the replies' nu ...
- PHP基础设计模式——工厂模式
<?php//文件名:Factory namespace IMooc; class Factory { //工程模式 static function creatDatabase() { $db ...
- 禁用menu键
发现很多应用中基本不再使用menu键来显示菜单了,而在android studio中创建一个activity时,默认使用了actionBar,就是在右上角会显示三个点的内容,点击会出现Settings ...
- Oracle insert update 时间处理
24小时表示方法:to_date(’ ::’,’yyyy-mm-dd hh24:mi:ss’) 12小时表示方法:to_date(’ ::’,’yyyy-mm-dd hh:mi:ss’) ','S75 ...
- ubuntu14.04 + cocos2d-x-3.6 + eclipse发布android
cocos2d-x-2.2.6版本 :http://www.cnblogs.com/weishuan/p/4698470.html 接下来是3.6了 ,准备好下面四个东东,我把这些都放在XXX/App ...
- 原生javascript 改写的tab选项卡
<!--css部分--> <style> *{ margin: 0; padding: 0; } ul,li{ list-style: none } .tabbox{ widt ...
- laravel-模板引擎Blade
(慕课网_轻松学会Laravel-基础篇_天秤vs永恒老师) 一.概述 Blade是Laravel提供的一个既简单又强大的模板引擎 和其他流行的PHP模板引擎不一样,Blade并不限制你在视图view ...
- Could not load the Tomcat server configuration at /Servers/Tomcat v7.0 Server at localhost-config.
[问题] Eclipse[Ubuntu14.04]中启动Tomcat Server[7.0.55]的时候出现错误例如以下: [解决方法] 1.将下边的Servers中的server[Tomcat v7 ...
- javascript 生成UUID
代码一: /*! Math.uuid.js (v1.4) http://www.broofa.com mailto:robert@broofa.com Copyright (c) 2010 Rober ...