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 解答的更多相关文章

  1. 42. Subsets && Subsets II

    Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...

  2. 【leetcode】Subsets II

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  3. 90. Subsets II

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  4. 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 ...

  5. Subsets II - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Subsets II - LeetCode 注意点 有重复的数字 数组可能是无序的,要先排序 解法 解法一:递归,只需要在Subsets中递归写法的基础上 ...

  6. 【LeetCode】90. Subsets II (2 solutions)

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  7. leetcode 78. Subsets 、90. Subsets II

    第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...

  8. 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 ...

  9. [Leetcode Week8]Subsets II

    Subsets II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/subsets-ii/description/ Description Given ...

随机推荐

  1. Codeforce 219 div1

    B 4D"部分和"问题,相当于2D部分和的拓展,我是分解成2D部分和做的: f[x1][y1][x2][y2]=true/false 表示 左上(x1,y1) 右下(x2,y2)的 ...

  2. 70个经典的 Shell 脚本面试问题

    转载自:http://www.imooc.com/article/1131 1) 如何向脚本传递参数 ? ./script argument 例子: 显示文件名称脚本 ./show.sh file1. ...

  3. 【转】TI蓝牙BLE 协议栈代码学习

    BLE就是低功率蓝牙.要着重了解两种设备: dual-mode双模设备:简单说就是向下兼容. single-mode单模设备:仅仅支持BLE.   关于开发主要讲的是单模设备,它可以只靠纽扣电池即可持 ...

  4. Understanding Abstractions of Secure Channels 的研读

  5. css3标签

    -moz代表firefox浏览器私有属性 -ms代表ie浏览器私有属性 -webkit代表chrome.safari私有属性 -o代表opera私有属性 border-radius:2em; 向div ...

  6. DataTable复制自身行

    在我们工作的过程中有可能要使用DataTable产生一些重复数据(在不重复读取数据库的情况下) 无废话,直接上代码 DataTable复制自身一行(目的产生重复数据),已测试通过可直接复制 /// & ...

  7. android 5.0 受欢迎的API简介

    android 5.0 作为系统的一次重大升级,给用户和开发者带来了全新的体验.Material Design不但在视觉和操作上更胜一筹,扩展UI工具包同时也引入了大量新的API. 1. 3D视图和实 ...

  8. FineUI控件之树的应用(二)

    一.Tree控件应用 <f:PageManager ID="PageManager1" runat="server" /> <f:Tree I ...

  9. unison实时双向数据同步

    软件下载 ocamlopt下载地址:http://caml.inria.fr Unison下载地址:http://www.seas.upenn.edu/~bcpierce/unison 1.安装uni ...

  10. Linux虚机centos6.5安装Vmware Tools步骤

    退出到windows,在虚拟机菜单栏中点击 虚拟机-> 安装 VMWARE TOOLS 子菜单 进入到Linux系统,选择系统工具-终端  命令 su root 或者 su 以root进入 挂在 ...