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 ...
随机推荐
- Corrupted MAC on input
Corrupted MAC on input Incorrect MAC received on packet
- Ubuntu mysql安装,还有可视化界面
安装mysql sudo apt-get install mysql-server sudo apt-get install mysql-client sudo apt-get install lib ...
- Hive 1、什么是Hive,Hive有什么用
一.什么是Hive Hive是建立在 Hadoop 上的数据仓库基础构架.它提供了一系列的工具,可以用来进行数据提取转化加载(ETL),这是一种可以存储.查询和分析存储在 Hadoop 中的大规模数据 ...
- NOTIFYICONDATA结构
//农机调度项目代码 NOTIFYICONDATA m_notifyData; m_notifyIcon.ChangeIcon(IDI_PAUSE, _T("监控终端server已暂停&qu ...
- js推断元素是否隐藏
if( document.getElementById("div").css("display")==='none') if( document.getEl ...
- leetcode:Minimum Path Sum(路线上元素和的最小值)【面试算法题】
题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...
- spring+hibernate整合:报错org.hibernate.HibernateException: No Session found for current thread
spring+hibernate整合:报错信息如下 org.hibernate.HibernateException: No Session found for current thread at o ...
- Linux 常用系统命令-20160504
一.显示目录和文件的命令 1.ls(list) 功能说明: 列出目录内容. 语 法 : ls [-1aAbBcCdDfFgGhHiklLmnNopqQrRsStuUvxX][-I < 范 本 ...
- VCS仿真生成fsdb文件(Verilog)
VCS仿真生成fsdb文件(Verilog) 一.环境 Linux 平台 csh环境 VCS 64bit Verdi3 二.开始仿真 1. 联合仿真环境配置 a.在testbench中加入如下语句: ...
- Javascript进阶篇——(DOM—getAttribute()、setAttribute()方法)—笔记整理
getAttribute()方法通过元素节点的属性名称获取属性的值.语法: elementNode.getAttribute(name) 1. elementNode:使用getElementById ...