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. 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---15

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:

  2. IDEA 2017.3 新版本中创建 JSF Web 应用程序缺少 web.xml 的解决办法

    IDEA 2017.3 新版本中默认创建一个 Web 应用程序很可能不会自动创建 web.xml 文件.虽然说从 JavaEE 6.0 开始 Servlet 3.0 规范中就新增了一些注解可以免去传统 ...

  3. LeetCode OJ--Combination Sum **

    https://oj.leetcode.com/problems/combination-sum/ 给一列数,3 2 1 3 3 8 7 9 ,每个数可以重复多次,给target 7, 问可以加起来得 ...

  4. C#使用SSDB管理增量日志并提供查询

    Program.cs using System; using System.Text; using CommonLinkLibrary.Util; using Newtonsoft.Json; nam ...

  5. 关于WEB页面的强制分页打印问题

    最近项目中有个需求要求打印web页面数据,但是碰到打印预览显示数据时,多的数据就不翼而飞了(不分页),搞的很是焦灼~ 最先是以为纸张的问题,胡乱折腾了一番,把A4约硬是改成了LARGE号的纸,多的数据 ...

  6. react Native 运行报错之一 gradle-2.14.1-all解压失败的问题

    在react-native run-android  时 可能出现压缩包不成功的情况,原因是压缩包失败,需要手动解压到相应目录, 目录:C:\Users\Administrator\.gradle\w ...

  7. centos7安装hadoop2.6.1,详细教程

    1.我搭建的是三台centos7的环境 首先准备三个centos7(文中出现的所有的链接都是我自己的) centos7下载地址(也可以上官网自行下载):https://pan.baidu.com/s/ ...

  8. ajax简单案例

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  9. Aria2使用教程

    Aria2 是一个轻量级多协议和多源命令行下载实用工具.它支持 HTTP / HTTPS, FTP, SFTP, bt 和 Metalink.通过内置 Aria2 可以操作 json - rpc 和 ...

  10. SQLITE3 --详解

    由于我主要负责我们小组项目数据库模块的部分所以这几天都一直在研究在iphone中最为常用的一个简单数据库sqlite,自己也搜集很多资料,因此在 这里总结一下这几天的学习成果: Sqlite 操作简明 ...