Subset II leetcode java
题目:
Given a collection of integers that might contain duplicates, S, 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 S = [1,2,2]
, a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
] 题解:
这个在subset题的第一种解法的基础上有两种解决办法。。
1. 在添加res时候判断是否res中已经存过该item了。没存过的才存保证子集唯一性。
代码如下:
1 public static void dfs(int[] S, int start, int len, ArrayList<Integer> item,ArrayList<ArrayList<Integer>> res){
2 if(item.size()==len){
3 if(!res.contains(item))
4 res.add(new ArrayList<Integer>(item));
5 return;
6 }
7 for(int i=start; i<S.length;i++){
8 item.add(S[i]);
9 dfs(S, i+1, len, item, res);
item.remove(item.size()-1);
}
}
public static ArrayList<ArrayList<Integer>> subsetsWithDup(int[] S) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>> ();
ArrayList<Integer> item = new ArrayList<Integer>();
if(S.length==0||S==null)
return res;
Arrays.sort(S);
for(int len = 1; len<= S.length; len++)
dfs(S,0,len,item,res);
res.add(new ArrayList<Integer>());
return res;
}
2. 还有一种方法就是在DFS过程中 当有重复元素出现就只对当前这个元素走一起,其他重复元素跳过。参考:http://blog.csdn.net/worldwindjp/article/details/23300545 代码如下:
1 public static void dfs(int[] S, int start, int len, ArrayList<Integer> item,ArrayList<ArrayList<Integer>> res){
2 if(item.size()==len){
3 res.add(new ArrayList<Integer>(item));
4 return;
5 }
6 for(int i=start; i<S.length;i++){
7 item.add(S[i]);
8 dfs(S, i+1, len, item, res);
9 item.remove(item.size()-1);
while(i<S.length-1&&S[i]==S[i+1])//跳过重复元素
i++;
}
}
public static ArrayList<ArrayList<Integer>> subsetsWithDup(int[] S) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>> ();
ArrayList<Integer> item = new ArrayList<Integer>();
if(S.length==0||S==null)
return res;
Arrays.sort(S);
for(int len = 1; len<= S.length; len++)
dfs(S,0,len,item,res);
res.add(new ArrayList<Integer>());
return res;
}
Subset II leetcode java的更多相关文章
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- Single Number II leetcode java
问题描述: Given an array of integers, every element appears three times except for one. Find that single ...
- Word Break II leetcode java
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- Palindrome Partitioning II Leetcode java
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- Remove Duplicates from Sorted List II leetcode java
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- Permutations II leetcode java
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- Ugly Number II leetcode java
问题描述: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fa ...
- Word Ladder II leetcode java
题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- Binary Tree Level Order Traversal II leetcode java
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
随机推荐
- luoguP4000 斐波那契数列
题目链接 luoguP4000 斐波那契数列 题解 根据这个东西 https://www.cnblogs.com/sssy/p/9418732.html 我们可以找出%p意义下的循环节 然后就可以做了 ...
- BZOJ.5312.冒险(线段树)
题目链接 \(Description\) 维护一个序列,支持区间and/or一个数.区间查询最大值. \(Solution\) 维护区间最大值?好像没什么用,修改的时候和暴力差不多. 我们发现有时候区 ...
- 简单单层bp神经网络
单层bp神经网络是解决线性可回归问题的. 该代码是论文:https://medium.com/technology-invention-and-more/how-to-build-a-simple-n ...
- Ulipad安装、配置使用教程(附Ulipad下载)
一.安装Ulipad 因为ulipad编辑器使用的是wxpython编写的gui,所以我们需要第三方库wxpython的支持,这里我们先讲一下Ulipad在Windows系统环境下的安装: 1. 确实 ...
- 青客宝团队Consul内部分享ppt
青客宝团队Consul内部分享ppt https://mp.weixin.qq.com/s?src=3×tamp=1503647705&ver=1&signatu ...
- 7款精美HTML5应用
1,HTML5/jQuery雷达动画图表图表配置十分简单 分享一款很特别的HTML5图表,它是利用HTML5和jQuery的雷达动画图表,图表数据在初始化的时候带有一定动画. 在线演示 ...
- shu_1016 栈
cid=1079&pid=2">http://202.121.199.212/JudgeOnline/problem.php?cid=1079&pid=2 分析: Ca ...
- 分享下使用 svn,测试服务器代码自动更新、线上服务器代码手动更新的配置经验
分享下使用 svn,测试服务器代码自动更新.线上服务器代码手动更新的配置经验 利用SVN的POST-COMMIT钩子自动部署代码 Linux SVN 命令详解 Linux SVN 命令详解2 使用sv ...
- delphi Image处理
procedure ImageDrawText(ATextEdo: IGGCCADTextEDO); var oImageBitmap: TBitmap; x1,x2,y1,y2: double; b ...
- [Asp.net core]使用Polly网络请求异常重试
摘要 在网络传输过程中,不能保证所有的请求都能正确的被服务端接受或者处理,那么进行简单的重试可以进行简单的补救.比如现在大部分支付功能,在支付成功之后,需要回调我们网站的接口,并且要求我们的接口给一个 ...