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 ...
随机推荐
- About Adultism and why things ar the way they are
About - Adultism About Adultism and why things ar the way they are In this page we will try to clari ...
- tool - 支持TestLink 1.93,将excel格式用例转化成可以导入的xml格式
tool - 支持TestLink 1.93,将excel格式用例转化成可以导入的xml格式 https://github.com/zhangzheyuk/CaseConvert
- IIS PHP 配置 问题总结
今天帮助朋友解决一个IIS配置PHP的问题.大概是这样子的. IIS 与 PHP配置好了之后不能訪问,出现例如以下错误: HTTP 错误 500.19 - Internal Server Error ...
- JMeter简单的性能测试实例
JMeter基础之——一个简单的性能测试 我们了解了jmeter的一此主要元件,那么这些元件如何使用到性能测试中呢.这一节创建一个简单的测试计划来使用这些元件.该计划对应的测试需求. 1)测试目标网站 ...
- [Redux] Extracting Container Components (FilterLink)
Learn how to avoid the boilerplate of passing the props down the intermediate components by introduc ...
- Sftp和ftp 差别、工作原理等(汇总ing)
Sftp和ftp over ssh2的差别 近期使用SecureFx,涉及了两个不同的安全文件传输协议: -sftp -ftp over SSH2 这两种协议是不同的.sftp是ssh内含的协议,仅仅 ...
- oracle卸载Oracle Clusterware(转载)
1.脚本自动删除 切换到root用户 $Su – root #cd $ORA_CRS_HOME/install 1.执行rootdelete.sh脚本 # ./rootdelete.sh 2.执行ro ...
- 监控工具cacti
一. 安装 cacti服务端 1. 首先要安装epel扩展源yum install -y epel-release2. (lamp)然后分别安装httpd.php.mysqlyum install - ...
- CSS3绘制环形进度条
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- js数组 函数
js数组 filter(),map(),some(),every(),forEach(),lastIndexOf(),indexOf() 文章1:http://www.jb51.net/article ...