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:

  1. [
  2. [2],
  3. [1],
  4. [1,2,2],
  5. [2,2],
  6. [1,2],
  7. []
  8. ]

解题思路一:

偷懒做法,将Java for LeetCode 078 Subsets中的List换为Set即可通过测试,JAVA实现如下:

  1. public List<List<Integer>> subsetsWithDup(int[] nums) {
  2. Set<List<Integer>> list = new HashSet<List<Integer>>();
  3. list.add(new ArrayList<Integer>());
  4. Arrays.sort(nums);
  5. for(int i=1;i<=nums.length;i++)
  6. dfs(list, nums.length, i, 0,nums,-1);
  7. return new ArrayList(list);
  8. }
  9.  
  10. static List<Integer> alist = new ArrayList<Integer>();
  11.  
  12. static void dfs(Set<List<Integer>> list, int n, int k, int depth,int[] nums,int last) {
  13. if (depth >= k) {
  14. list.add(new ArrayList<Integer>(alist));
  15. return;
  16. }
  17. for (int i = last+1; i <= n-k+depth; i++) {
  18. alist.add(nums[i]);
  19. dfs(list, n, k, depth + 1,nums,i);
  20. alist.remove(alist.size() - 1);
  21. }
  22. }

解题思路二:

思路一其实用到了Java for LeetCode 077 CombinationsJava for LeetCode 078 Subsets的结论,使用set每次添加元素都需要查询一遍,会增加额外的时间开销,我们可以有一个不使用Set的解法,JAVA实现如下:

  1. public List<List<Integer>> subsetsWithDup(int[] S) {
  2. List<List<Integer>> res = new ArrayList<List<Integer>>();
  3. List<Integer> cur = new ArrayList<Integer>();
  4. Arrays.sort(S);
  5. dfs(0, res, cur, S);
  6. return res;
  7. }
  8.  
  9. private void dfs(int dep, List<List<Integer>> res, List<Integer> cur,
  10. int[] S) {
  11. if (dep == S.length) {
  12. res.add(new ArrayList<Integer>(cur));
  13. return;
  14. }
  15. int upper = dep;
  16. while (upper >= 0 && upper < S.length - 1 && S[upper] == S[upper + 1])
  17. upper++;
  18. dfs(upper + 1, res, cur, S);
  19. for (int i = dep; i <= upper; i++) {
  20. cur.add(new Integer(S[dep]));
  21. dfs(upper + 1, res, cur, S);
  22. }
  23. for (int i = dep; i <= upper; i++)
  24. cur.remove(cur.size() - 1);
  25. }

Java for LeetCode 090 Subsets II的更多相关文章

  1. [Leetcode Week8]Subsets II

    Subsets II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/subsets-ii/description/ Description Given ...

  2. 【leetcode】Subsets II

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  3. Java for LeetCode 047 Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  4. [LeetCode] 90.Subsets II tag: backtracking

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  5. [leetcode]90. Subsets II数组子集(有重)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  6. [LeetCode] 90. Subsets II 子集合 II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  7. [LeetCode]题解(python):090 Subsets II

    题目来源 https://leetcode.com/problems/subsets-ii/ Given a collection of integers that might contain dup ...

  8. LeetCode 90. Subsets II (子集合之二)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  9. Java for LeetCode 078 Subsets

    Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...

随机推荐

  1. Autolayout 03

    Debugging in Code 有两个调试layout问题的阶段. 1. Map from “this view is in the wrong place” to “this constrain ...

  2. Android Retrofit使用教程

    Square公司开源了许多优秀的库,Retrofit就是其中之一. Retrofit是用来简化APP访问服务器API,如果你的服务器使用的使RESTAPI,那么赶紧使用Retrofit吧. 官方的文档 ...

  3. pandas常用操作

    删除某列: concatdfs.drop('Unnamed: 0',axis=1) 打印所有列名: .columns

  4. 安装 Groovy

    brew install groovy http://wiki.jikexueyuan.com/project/groovy-introduction/install-groovy.html

  5. linux中脚本扑捉(trap)信号问题

    扑捉ctrl+c信号: #!/bin/bash trap ; function trap() { echo "You press Ctrl+C."; echo "Exit ...

  6. GlusterFS分布式文件系统高速管理

    TaoCloud XDFS基于GlusterFS开源分布式文件系统,进行了系统优化.project化.定制化和产品化工作,五年以上的实践积累了大量实践经验,包含客户案例.最佳实践.定制开发.咨询服务和 ...

  7. JAVA_StandardServer await create[8005]怎么办

    Tomcat 6.0 错误信息: 严重: StandardServer.await:create[8005]: java.net.BindException: Address already in u ...

  8. es6 includes(), startsWith(), endsWith()

    传统上,JavaScript 只有indexOf方法,可以用来确定一个字符串是否包含在另一个字符串中.ES6 又提供了三种新方法. includes():返回布尔值,表示是否找到了参数字符串. sta ...

  9. 可软件定义的存储逻辑——Efficient and agile storage management in software defined environments

            note:写这个或许算是翻译,又或算是对这个论文[1]的理解,又或者仅仅是我的看法.         这篇论文和IOFlow相比較,更加注重软件定义存储的框架(利用已有的框架来创建新的 ...

  10. 如何让DIV居中

    总结:text-align:center;对三中浏览器而言,都具有文字/行内元素的嵌套式居中,或者说继承式的居中,只要外面的容器设置了这个属性,那么他内部的所有元素都具有这个属性(意思是,虽然这个属性 ...