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. C#Qrcode生成二维码支持中文,带图片,带文字

    C#Qrcode生成二维码支持中文带图片的操作请看二楼的帖子,当然开始需要下载一下C#Qrcode的源码 下载地址 : http://www.codeproject.com/Articles/2057 ...

  2. 介绍Node.JS

    几年前,完全放弃Asp.net,彻底脱离微软方向.Web开发,在公司团队中,一概使用Node.js.Mongodb.Git,替换Asp.net mvc.Sql server和Tfs.当时来看,这是高风 ...

  3. UVA 272 TEX Quotes【字符串】

    https://vjudge.net/problem/UVA-272 [分析]:标记一下. [代码]: #include <bits/stdc++.h> using namespace s ...

  4. codevs——1048 石子归并 (区间DP)

    时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解       题目描述 Description 有n堆石子排成一列,每堆石子有一个重量w[i], 每次合并可以合并 ...

  5. codevs——1044 拦截导弹(序列DP)

    1999年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 某国为了防御敌国的导弹袭击 ...

  6. 【转】java8中谨慎使用实数作为HashMap的key!

    java8中谨慎使用实数作为HashMap的key! java8中一个hashCode()函数引发的血案java8中一个hashCode()函数引发的血案1.起因2.实数的hashCode()3.总结 ...

  7. iOS使用MD5 - 字符串加密至MD5&获取文件MD5

    iOS 字符串加密至MD5 + (NSString *) md5:(NSString *)str { unsigned ]; CC_MD5( cStr, strlen(cStr), result ); ...

  8. hibernate save update merge 区别

    1.save save的对象是临时对象,首先将对象写入缓存,使其变为持久对象. save函数底层使用的是Insert语句,因此如果数据库中已经存在相同ID的记录,那么会报错 2.update upda ...

  9. python 制作wordcloud词云

    pip install wordcloud 需要用到numpy  pillow matplotlib 安装完成以后 wordcloud_cli --text in.txt --imagefile ou ...

  10. 【原创】SM4password算法源代码接口具体解释

    [原创]SM4password算法源代码接口具体解释 近期几天想把cryptdb的加密算法换成国产的sm4加密算法.所以花了时间研究了一下sm4的源代码和基本原理,避免忘记,写下这篇博客以作记录. 先 ...