Java for LeetCode 090 Subsets II
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 Combinations和Java 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的更多相关文章
- [Leetcode Week8]Subsets II
Subsets II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/subsets-ii/description/ Description Given ...
- 【leetcode】Subsets II
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- Java for LeetCode 047 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] 90.Subsets II tag: backtracking
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [leetcode]90. Subsets II数组子集(有重)
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [LeetCode] 90. Subsets II 子集合 II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [LeetCode]题解(python):090 Subsets II
题目来源 https://leetcode.com/problems/subsets-ii/ Given a collection of integers that might contain dup ...
- LeetCode 90. Subsets II (子集合之二)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- Java for LeetCode 078 Subsets
Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...
随机推荐
- Autolayout 03
Debugging in Code 有两个调试layout问题的阶段. 1. Map from “this view is in the wrong place” to “this constrain ...
- Android Retrofit使用教程
Square公司开源了许多优秀的库,Retrofit就是其中之一. Retrofit是用来简化APP访问服务器API,如果你的服务器使用的使RESTAPI,那么赶紧使用Retrofit吧. 官方的文档 ...
- pandas常用操作
删除某列: concatdfs.drop('Unnamed: 0',axis=1) 打印所有列名: .columns
- 安装 Groovy
brew install groovy http://wiki.jikexueyuan.com/project/groovy-introduction/install-groovy.html
- linux中脚本扑捉(trap)信号问题
扑捉ctrl+c信号: #!/bin/bash trap ; function trap() { echo "You press Ctrl+C."; echo "Exit ...
- GlusterFS分布式文件系统高速管理
TaoCloud XDFS基于GlusterFS开源分布式文件系统,进行了系统优化.project化.定制化和产品化工作,五年以上的实践积累了大量实践经验,包含客户案例.最佳实践.定制开发.咨询服务和 ...
- JAVA_StandardServer await create[8005]怎么办
Tomcat 6.0 错误信息: 严重: StandardServer.await:create[8005]: java.net.BindException: Address already in u ...
- es6 includes(), startsWith(), endsWith()
传统上,JavaScript 只有indexOf方法,可以用来确定一个字符串是否包含在另一个字符串中.ES6 又提供了三种新方法. includes():返回布尔值,表示是否找到了参数字符串. sta ...
- 可软件定义的存储逻辑——Efficient and agile storage management in software defined environments
note:写这个或许算是翻译,又或算是对这个论文[1]的理解,又或者仅仅是我的看法. 这篇论文和IOFlow相比較,更加注重软件定义存储的框架(利用已有的框架来创建新的 ...
- 如何让DIV居中
总结:text-align:center;对三中浏览器而言,都具有文字/行内元素的嵌套式居中,或者说继承式的居中,只要外面的容器设置了这个属性,那么他内部的所有元素都具有这个属性(意思是,虽然这个属性 ...