题目

Given a collection of integers that might contain duplicates, S, 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 S = [1,2,2], a solution is:

[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
] 题解
这个在subset题的第一种解法的基础上有两种解决办法。。
1. 在添加res时候判断是否res中已经存过该item了。没存过的才存保证子集唯一性。
代码如下:
 1 public static void dfs(int[] S, int start, int len, ArrayList<Integer> item,ArrayList<ArrayList<Integer>> res){
 2         if(item.size()==len){
 3             if(!res.contains(item))
 4                 res.add(new ArrayList<Integer>(item));
 5             return;
 6         }
 7         for(int i=start; i<S.length;i++){
 8             item.add(S[i]);
 9             dfs(S, i+1, len, item, res);
             item.remove(item.size()-1);
         }
 
     }
     
     public static ArrayList<ArrayList<Integer>> subsetsWithDup(int[] S) {
         ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>> ();
         ArrayList<Integer> item = new ArrayList<Integer>();
         if(S.length==0||S==null)
             return res;
         
         Arrays.sort(S);
         for(int len = 1; len<= S.length; len++)
             dfs(S,0,len,item,res);
             
         res.add(new ArrayList<Integer>());
         
         return res;
     }
2. 还有一种方法就是在DFS过程中 当有重复元素出现就只对当前这个元素走一起,其他重复元素跳过。参考:http://blog.csdn.net/worldwindjp/article/details/23300545

代码如下:
 1 public static void dfs(int[] S, int start, int len, ArrayList<Integer> item,ArrayList<ArrayList<Integer>> res){
 2         if(item.size()==len){
 3             res.add(new ArrayList<Integer>(item));
 4             return;
 5         }
 6         for(int i=start; i<S.length;i++){
 7             item.add(S[i]);
 8             dfs(S, i+1, len, item, res);
 9             item.remove(item.size()-1);
             while(i<S.length-1&&S[i]==S[i+1])//跳过重复元素
                 i++;
         }
 
     }
     
     public static ArrayList<ArrayList<Integer>> subsetsWithDup(int[] S) {
         ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>> ();
         ArrayList<Integer> item = new ArrayList<Integer>();
         if(S.length==0||S==null)
             return res;
         
         Arrays.sort(S);
         for(int len = 1; len<= S.length; len++)
             dfs(S,0,len,item,res);
             
         res.add(new ArrayList<Integer>());
         
         return res;
     }       
												

Subset II leetcode java的更多相关文章

  1. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  2. Single Number II leetcode java

    问题描述: Given an array of integers, every element appears three times except for one. Find that single ...

  3. Word Break II leetcode java

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  4. Palindrome Partitioning II Leetcode java

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  5. Remove Duplicates from Sorted List II leetcode java

    题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...

  6. Permutations II leetcode java

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  7. Ugly Number II leetcode java

    问题描述: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fa ...

  8. Word Ladder II leetcode java

    题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...

  9. Binary Tree Level Order Traversal II leetcode java

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

随机推荐

  1. 手把手教你做爬虫---基于NodeJs

    前言: 趁着北京今儿天气格外的蓝,我觉得我得干点什么,于是乎,卷起袖子,整理一下最近做爬虫的那些事儿. 目标:爬取北京大学软件与微电子学院的所有新闻,并将内容及图片存储到本地. 设计思路:经过对北京大 ...

  2. BZOJ3956: Count

    Description   Input   Output   Sample Input 3 2 0 2 1 2 1 1 1 3 Sample Output 0 3 HINT M,N<=3*10^ ...

  3. NOIP2015其余几道题

    T1: #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> # ...

  4. notepad++ 如何选择10000行-20000行之间的文本?

    最近要上传导入一批数据,但是数据太多,一次上传不了,所以就要分批上传,而且数据全部在一个txt里面,这时就想一次复制一部分出来导入,直到导入完成,但是问题来了,数据太多,选择1到10000行,鼠标要拉 ...

  5. 20172319 2018.10.12《Java程序设计教程》第6周课堂实践(补写博客)

    20172319 2018.10.12 <Java程序设计教程>第6周课堂测验 课程:<程序设计与数据结构> 班级:1723 学生:唐才铭 学号:20172319 指导老师:王 ...

  6. Gym 100646 Problem E: Su-Su-Sudoku 水题

    Problem E: Su-Su-Sudoku/center> 题目连接: http://codeforces.com/gym/100646/attachments Description By ...

  7. Delphi : Analyze PE file headers?

    Analyze PE file headers? { You'll need a OpenDialog to open a Exe-File and a Memo to show the file i ...

  8. STM32 GPIO fast data transfer with DMA

    AN2548 -- 使用 STM32F101xx 和 STM32F103xx 的 DMA 控制器 DMA控制器 DMA是AMBA的先进高性能总线(AHB)上的设备,它有2个AHB端口: 一个是从端口, ...

  9. [Asp.net mvc]Html.ValidationSummary(bool)

    摘要 对ValidationSummary是HtmlHelper的扩展方法,用来返回 System.Web.Mvc.ModelStateDictionary (即ModelState)对象中的验证消息 ...

  10. delphi时间日期函数

    unit DateProcess; interface const DayOfWeekStrings: ..] of String = ('SUNDAY', 'MONDAY', 'TUESDAY', ...