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 ...
随机推荐
- Android为什么方法数不能超过65535
言归正传,来聊聊为什么方法数不能超过65535?搬上Dalvik工程师在SF上的回答,因为在Dalvik指令集里,调用方法的invoke-kind指令中,method reference index只 ...
- mysql忘记密码的解决办法
mysql忘记密码时,需要重设密码. 在Windows下的操作如下: 1.关闭正在运行的MySQL. 2.打开DOS窗口,转到mysql\bin目录. 3.输入mysqld --skip-grant- ...
- 228. 汇总区间(leetcode)
#整体思路:使用堆栈,在Python中可以使用列表代替:如果a[i]-a[i-1]==1,就要将a[i]合并到之前的区间里,#所以我们队首位元素开辟一个区间为[a[0],a[0]]#做最后汇总时候,如 ...
- java基础篇4之注解
1 注解的应用(jdk1.5的新特性) 一个注解相当于一个特殊的类 例子: @SuppressWarning("deprecation") @Deprecated @Overrid ...
- Zookeeper协调分布式节点demo
多台服务器和客户端通过第三方组件Zookeeper管理 public class DistributedServer { private static final String connectStri ...
- linux设备驱动程序之并发和竞态(二)
事实上这blog都是阅读ldd3时的一些总结,巩固自己的学习.也方便后期的使用.大家也能够直接阅读ldd3原文. 锁陷阱 所谓的锁陷阱就是防止死锁. 不明白的规则: ...
- jquery方法
$.inArray(被判断的量,ArrayName); 如果存在返回索引值,如果不存在返回-1 $.unique() 数组去重 根据去重前后的长度,判断是否有重复 $.each(被遍历的数组,f ...
- SharePoint 2013 对话框
The quick way to open a sharepoint 2013 dialog modal form is via Javascript below 1 2 3 4 5 function ...
- java中的值传递和引用传递区别
值传递:(形式参数类型是基本数据类型):方法调用时,实际参数把它的值传递给对应的形式参数,形式参数只是用实际参数的值初始化自己的存储单元内容,是两个不同的存储单元,所以方法执行中形式参数值的改变不影响 ...
- linq查询去重
通过自定义扩展方法DistinctBy实现去重 public static IEnumerable<TSource> DistinctBy<TSource, TKey> (th ...