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],
[]
]

解题思路一:

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

public List<List<Integer>> subsetsWithDup(int[] nums) {
Set<List<Integer>> list = new HashSet<List<Integer>>();
list.add(new ArrayList<Integer>());
Arrays.sort(nums);
for(int i=1;i<=nums.length;i++)
dfs(list, nums.length, i, 0,nums,-1);
return new ArrayList(list);
} static List<Integer> alist = new ArrayList<Integer>(); static void dfs(Set<List<Integer>> list, int n, int k, int depth,int[] nums,int last) {
if (depth >= k) {
list.add(new ArrayList<Integer>(alist));
return;
}
for (int i = last+1; i <= n-k+depth; i++) {
alist.add(nums[i]);
dfs(list, n, k, depth + 1,nums,i);
alist.remove(alist.size() - 1);
}
}

解题思路二:

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

	public List<List<Integer>> subsetsWithDup(int[] S) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<Integer> cur = new ArrayList<Integer>();
Arrays.sort(S);
dfs(0, res, cur, S);
return res;
} private void dfs(int dep, List<List<Integer>> res, List<Integer> cur,
int[] S) {
if (dep == S.length) {
res.add(new ArrayList<Integer>(cur));
return;
}
int upper = dep;
while (upper >= 0 && upper < S.length - 1 && S[upper] == S[upper + 1])
upper++;
dfs(upper + 1, res, cur, S);
for (int i = dep; i <= upper; i++) {
cur.add(new Integer(S[dep]));
dfs(upper + 1, res, cur, S);
}
for (int i = dep; i <= upper; i++)
cur.remove(cur.size() - 1);
}

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. spring与struts2整合出现常见错误

    错误信息 严重: Exception starting filter struts2 Unable to load configuration. - bean - jar:file:/F:/Strut ...

  2. centos使用yum安装gcc

    yum -y install gccyum -y install gcc-c++yum install make -- 或者yum groupinstall "Development Too ...

  3. xampp添加 django支持

    apache配置文件中添加 WSGIScriptAlias /chatbot1 /Users/css/djangoprojects/chatbot1/chatbot1/wsgi.pyWSGIPytho ...

  4. 2016.8.19 将div设置为隐藏使用style=“display:none”

    style="display:none"表示隐藏. style="display:block"表示显示. 在代码中则使用$("#id").s ...

  5. VIM正则表达式查找替换

      0. 一些需要注意的不同 VIM中的正则表达式和其他的有点不一样 (1) 有些符号要用\转义,比如\+表示重复一次或以上,其他的还有一些,:h pattern查看(2) 非贪婪匹配用\{-}, 如 ...

  6. BZOJ 1878 SDOI2009 HH的项链 树状数组/莫队算法

    题目大意:给定一个序列.求一个区间内有多少个不同的数 正解是树状数组 将全部区间依照左端点排序 然后每次仅仅统计左端点開始的每种颜色的第一个数即可了 用树状数组维护 我写的是莫队算法 莫队明显能搞 m ...

  7. Android内存泄露调试

    Android 内存泄漏调试 一.概述 如果我们编写的代码当中有太多的对内存使用不当的地方,难免会使得我们的设备运行缓慢,甚至是死机.为了能够使得 Android 应用程序安全且快速的运行, Andr ...

  8. HTML字体对应word字体

    42磅对应初号. 36磅对应小初. 26磅对应一号. 24磅对应小一号. 22磅对应二号. 18磅对应小二号. 16磅对应三号. 15磅对应小三号. 14磅对应四号. 12磅对应小四号. 10.5磅对 ...

  9. 仿VS安装界面小球滑动效果

    在Visual Studio 2010后续版本的安装界面中,可以发现一组小球在滑动表示安装程序正在进行: 于是尝试用CSS实现了一下. 首先需要建立用来表示小球的html结构: <div cla ...

  10. thymeleaf模版的使用

    thymeleaf,我个人认为是个比较好的模板,性能也比一般的,比如freemaker的要高,而且把将美工和程序员能够结合起来,美工能够在浏览器中查看静态效果,程序员可以在应用服务器查看带数据的效果. ...