题目

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. 网页图表Highcharts实践教程之标签组与载入动画

    网页图表Highcharts实践教程之标签组与载入动画 Highcharts标签组 在图表的大部分元素都提供了标签功能.但很多时候,我们需要额外说明一些信息.这个时候借助原有的图表元素的标签功能就不是 ...

  2. BZOJ.4946.[NOI2017]蔬菜(贪心 离线)

    题目链接 因为有删除,考虑倒序处理某个p的询问. 那么每天删除xi的蔬菜就变成了每天运来xi的蔬菜.那么我们取当前最优的即可,早取晚取都一样,不需要留给后面取,还能给后面更优的留出空间. 这样就只需考 ...

  3. BZOJ.1021.[SHOI2008]循环的债务(DP)

    题目链接 不同面额的钞票是可以分开考虑的. ↑其实并不很明白具体(证明?),反正是可以像背包一样去做. f[x][i][j]表示用前x种面额钞票满足 A有i元 B有j元 (C有sum-i-j)所需交换 ...

  4. hdu 4451 37届金华赛区 J题

    题意:给出衣服裤子鞋子的数目,有一些衣服和裤子,裤子和鞋子不能搭配,求最终的搭配方案总数 wa点很多,我写wa了很多次,代码能力需要进一步提升 #include<cstdio> #incl ...

  5. [Java]struts2-spring整合时配置监听器

    1.struts2-spring整合时配置监听器 [在web.xml中] <!-- 上下文载入器监听器,确保web服务器启动时,直接完成spring容器的初始化 --/> [Ctrl + ...

  6. PHP 图像居中裁剪函数

    图像居中裁减的大致思路: 1.首先将图像进行缩放,使得缩放后的图像能够恰好覆盖裁减区域.(imagecopyresampled — 重采样拷贝部分图像并调整大小) 2.将缩放后的图像放置在裁减区域中间 ...

  7. ROS知识(22)----USB口映射固定名字

    如果有多个usb链接到电脑,如果插入的先后顺序不同,那么会导致对应的usb口也会不同,例如当只有一个激光的usb链接到电脑,其设备名字为/dev/ttyUSB0:当如果有底盘的usb以及激光的usb连 ...

  8. Jmeter关于上传图片接口

    最近接到的一个新的项目,老规矩,开发组开发完接口需要进行接口的测试,其他的很简单,根据限制条件逻辑等设计数据,用浏览器或者工具进行验证就OK. 其中有一个接口涉及到图片的上传,以前没有用过,通过查找资 ...

  9. OpenVPN的ipp.txt为空不生效的问题

    1.ipp.txt是分配固定IP使用的,但在tun模式下里面的ip地址不是写使用着的IP,而是30位子网中没有列举出来的启动一位,比如我要给客户机分配为10.8.0.6的IP,那么它这个文件填写的是1 ...

  10. 安装oracle10g“程序异常终止。发生内部错误。请将以下文件提供给oracle技术支持部门

    发生情景:测试环境搭建的是windows 2008 r2 sp1系统 在安装Oracle 10g数据库时发生了错误,现在把解决问题的方法和原因分享给大家. *  安装出现的现象: 输入完密码后下一步时 ...