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 ...
随机推荐
- 不得不知Git远程操作详解
Git是目前最流行的版本管理系统,学会Git几乎成了开发者的必备技能. Git有很多优势,其中之一就是远程操作非常简便.本文详细介绍5个Git命令,它们的概念和用法,理解了这些内容,你就会完全掌握Gi ...
- Django 创建APP - 简单路由系统案例
架构图: setting.py: INSTALLED_APPS = [ ... 'bootstrap', ] myapp -> myapp -> urls.py from django.c ...
- 【C语言天天练(十一)】深入理解指针
引言:在C语言中.指针的地位是不言而喻的,要想非常好的掌握C语言,掌握指针是必须的,这也是C语言不同于其它语言的地方. (一)指针的指针 样例: int i; int *pi;/*把pi初始化为指向变 ...
- 【VBS】使用Visual Studio调试VBS程序
首先要确保机器上安装了Visual Stuido, 然后打开命令行窗口执行如下命令,会弹出是否使用Visual Studio进行调试的确认窗口. 点[是]进行调试. WScript.exe [vbs文 ...
- 一个免费的API-手机号码归属地接口
手机号码归属地接口:根据手机号码或手机号码的前7位,查询手机号码归属地信息,包括省份 .城市.区号.邮编.运营商和卡类型. 接口文档:https://www.juhe.cn/docs/api/id/1 ...
- java 逻辑运算符 短路(条件操作)
两个数字计算时都会先把数字转换成二进制后再进行换算,二进制就是由0和1组成的数字 http://yxwang0615.iteye.com/blog/1084288
- NodeJS 安装cnpm命令行工具
在安装之前,请确保已安装Git和NodeJS. cmd机内命令窗口,输入以下命令: git config --system http.sslcainfo /bin/curl-ca-bundle.crt ...
- VueJS组件之全局组件与局部组件
全局组件 所有实例都能用全局组件. HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...
- linux下robotframework执行测试用例的几种方法
1.执行指定的测试用例文件(Test Suite) [root@localhost cases]# pybot purge.txt 2.执行整个porject目录下的所有测试用例 ...
- 分布式搜索elasticsearch 环境搭建
1.elasticsearch安装 elasticsearch的安装超级easy,解压即用(要事先安装好java环境). 到官网 http://www.elasticsearch.org下载最新版的 ...