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 ...
随机推荐
- poj3254:基础状压dp
第二个状压dp 做过的第一个也是放牛问题,两头牛不能相邻 这个题多了一个限制,就是有些位置不能放牛 于是先与处理一下每一行所有不能放牛的状态,处理的过程直接对每一个不能放牛的状态或以下 ac代码: # ...
- Struts2简单例子
Struts实现注册功能 ControlFilter.java package com.jikexueyuan.filter; import java.io.IOException; import j ...
- hdu 4135 Co-prime(容斥)
Co-prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- WebService-使用JDK开发WebService
一.使用JDK开发WebService 2.1.开发WebService服务器端 1.定义一个interface,使用@WebService注解标注接口,使用@WebMethod注解标注接口中定义的所 ...
- ARM指令集——条件执行、内存操作指令、跳转指令
ARM 汇编指令条件执行 在ARM模式下,任何一条数据处理指令可以选择是否根据操作的结果来更新CPSR寄存器中的ALU状态标志位.在数据处理指令中使用S后缀来实现该功能. 不要在CMP,CMN,TST ...
- Android下Affinities和Task
源文链接:http://appmem.com/archives/405 本文参考了官方Dev Guide文档,简单介绍Android下的affinities和任务(task). 1.Activity和 ...
- VCS仿真生成vpd文件(verilog)
VCS仿真生成vpd文件(verilog) 一.环境与文件 Linux平台 csh环境 VCS 64bit 代码文件请参考<一个简单的Verilog计数器模型> 二.开始仿真 1.com ...
- 【转】Objective-C消息机制的原理
转自:http://dangpu.sinaapp.com/?p=119 在Objective-C中,message与方法的真正实现是在执行阶段绑定的,而非编译阶段.编译器会将消息发送转换成对objc_ ...
- 文本输入框的两种div+css的写法
1.扁平化的设计风格.--淘宝 直接使用input.对其设置height.padding.使鼠标居中又不会占满输入框. 2.背景是图片的设计.--百度 试用span将input包裹 ...
- C#图像处理(2):给图片加白边
C#图片处理给图片添加白边: /// <summary> /// 在图片上方加入白边 /// </summary> /// <param name="Img&q ...