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],
[]
]

思路:这一题比subsets多一了一道反复,详细代码例如以下:

public class Solution {
boolean[] b;
Set<String> set;
List<List<Integer>> list;
Set<String> set1;
public List<List<Integer>> subsetsWithDup(int[] nums) {
b = new boolean[nums.length];
set = new HashSet<String>();
list = new ArrayList<List<Integer>>();
set1 = new HashSet<String>(); Arrays.sort(nums);
count(nums,"",nums.length,0);
return list;
} private void count(int[] nums,String s,int n,int j){
//没有反复才加入
if(set.add(s)){
//以","切割数组
String[] sa = s.split(",");
List<Integer> al = new ArrayList<Integer>();
for(int i = 0; i < sa.length; i++){
if(sa[i].length() > 0){
al.add(Integer.parseInt(sa[i]));
}
}
Collections.sort(al);
if(set1.add(al.toString()))
list.add(al);
} for(int i = j; i < nums.length;i++){
if(!b[i]){
b[i] = true;
count(nums,s + "," + nums[i],n-1,i+1);
b[i] = false;
}
}
} }

以下这样的写法更简洁:

public class Solution {
List<List<Integer>> list;//结果集
List<Integer> al;//每一项
public List<List<Integer>> subsetsWithDup(int[] nums) {
list = new ArrayList<List<Integer>>();
al = new ArrayList<Integer>();
Arrays.sort(nums);
//排列组合
count(nums,al,0);
return list;
} private void count(int[] nums,List<Integer> al,int j){ list.add(new ArrayList<Integer>(al));//不反复的才加入 for(int i = j; i < nums.length;i++){
if(i == j || nums[i] != nums[i-1]){//去除反复
al.add(nums[i]);//加入
count(nums,al,i+1);
al.remove(al.size()-1);//去除。为下一个结果做准备
}
}
} }

leetCode 90.Subsets II(子集II) 解题思路和方法的更多相关文章

  1. leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  2. leetcode 113. Path Sum II (路径和) 解题思路和方法

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  3. leetCode 86.Partition List(分区链表) 解题思路和方法

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  4. leetCode 75.Sort Colors (颜色排序) 解题思路和方法

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  5. leetCode 15. 3Sum (3数之和) 解题思路和方法

    3Sum  Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...

  6. leetCode 57.Insert Interval (插入区间) 解题思路和方法

    Insert Interval  Given a set of non-overlapping intervals, insert a new interval into the intervals ...

  7. leetCode 61.Rotate List (旋转链表) 解题思路和方法

    Rotate List  Given a list, rotate the list to the right by k places, where k is non-negative. For ex ...

  8. leetCode 67.Add Binary (二进制加法) 解题思路和方法

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

  9. leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  10. leetCode 66.Plus One (+1问题) 解题思路和方法

    Plus One Given a non-negative number represented as an array of digits, plus one to the number. The ...

随机推荐

  1. FTK应用程序编程接口(API)手册-1【转】

    转自:http://blog.csdn.net/absurd/article/details/6702047 框架函数 框架函数支撑FTK的主体,它负责初始化应用程序,启动主循环和退出主循环.它对任何 ...

  2. [深度应用]·Keras实现Self-Attention文本分类(机器如何读懂人心)

    [深度应用]·Keras实现Self-Attention文本分类(机器如何读懂人心) 配合阅读: [深度概念]·Attention机制概念学习笔记 [TensorFlow深度学习深入]实战三·分别使用 ...

  3. openshift scc解析

    SCC使用UserID,FsGroupID以及supplemental group ID和SELinux label等策略,通过校验Pod定义的ID是否在有效范围内来限制pod的权限.如果校验失败,则 ...

  4. Ruby Time And DateTime之Time in Core

    今天遇到一个问题,就是在Ruby中对于Time和DateTime的使用,不是很明了,现在研究一下: 先说Time: 在Ruby2.0中关于Time有两处定义一个是在Core中,http://www.r ...

  5. Jenkins插件HTML Publisher Plugin的使用

    前提: 下载插件HTML Publisher plugin 一.安装 安装好HTML Publisher plugin之后,会在新建或者编辑项目时,在[增加构建后操作步骤]出现[Publish HTM ...

  6. Mac git 的使用

    1. mac 安装git brew install git 2.初使化 git config --global user.name "mygit" git config --glo ...

  7. SQL Server数据库连接,Web.config的正确配置 [转]

    http://database.51cto.com/art/201007/213289.htm 此文章主要介绍的是Web.config正确配置SQL Server数据库连接的实际擦步骤,在图5-6中, ...

  8. 用循环将三个DIV变成红色

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. mysqldumps 远程备份

    普通模式 mysqldump -uroot -ppassword -h10.26.114.25 -P3306 --databases databasename > XXX.sql 多条在一起模式 ...

  10. 关于使用Axure RP进行原型开发的一些心得体会

    Axure RP(Axure Rapid Prototyping)是一款高速实现.准确表达.带有交互效果且易于上手的原型设计工具. 本人在曾參与某系统需求分析时開始接触Axure RP,初步掌握了一定 ...