90. Subsets II
2016-07-20 09:45
363 查看
Given a collection of integers that might contain duplicates, nums, return all possible subsets.
Note: The solution set must not contain duplicate subsets.
For example,
If nums =
a solution is:
二进制法生成所有子集合,hash去重,生成方法优,去重耗费大量时间,是下策
public class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums)
{
int len=nums.length;
List<List<Integer>> retlist=new ArrayList<>(1<<len);
HashSet<ArrayList<Integer>> hashset=new HashSet<>(256);
Arrays.sort(nums);
for(int i=0;i<1<<len;i++)
{
ArrayList<Integer> arraylist=new ArrayList<>(len);
for(int j=0;j<len;j++)
if((i&(1<<j))!=0)
arraylist.add(nums[j]);
if(!hashset.contains(arraylist))
{
retlist.add(arraylist);
hashset.add(arraylist);
}
}
return retlist;
}
}
回溯法,设nums长度为n,搜索深度依次从0到n,先排序从上次搜索下个位置开始搜,相同的元素捆绑处理。
public class Solution {
List<List<Integer>> retlist=new ArrayList<>(256);
ArrayList<Integer> arraylist;
public List<List<Integer>> subsetsWithDup(int[] nums)
{
int len=nums.length;
arraylist=new ArrayList<>(512);
Arrays.sort(nums);
for(int i=0;i<=len;i++)
dfs(i, 0, -1, nums);
return retlist;
}
public void dfs(int target,int deep,int lastindex,int[] nums)
{
if(deep==target)
{
retlist.add(new ArrayList<>(arraylist));
return ;
}
for(int i=lastindex+1;i<nums.length;i++)
{
if(i>0&&nums[i]==nums[i-1])
if(lastindex!=i-1)
continue;
arraylist.add(nums[i]);
dfs(target, deep+1, i, nums);
arraylist.remove(arraylist.size()-1);
}
}
}
Note: 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], [] ]
二进制法生成所有子集合,hash去重,生成方法优,去重耗费大量时间,是下策
public class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums)
{
int len=nums.length;
List<List<Integer>> retlist=new ArrayList<>(1<<len);
HashSet<ArrayList<Integer>> hashset=new HashSet<>(256);
Arrays.sort(nums);
for(int i=0;i<1<<len;i++)
{
ArrayList<Integer> arraylist=new ArrayList<>(len);
for(int j=0;j<len;j++)
if((i&(1<<j))!=0)
arraylist.add(nums[j]);
if(!hashset.contains(arraylist))
{
retlist.add(arraylist);
hashset.add(arraylist);
}
}
return retlist;
}
}
回溯法,设nums长度为n,搜索深度依次从0到n,先排序从上次搜索下个位置开始搜,相同的元素捆绑处理。
public class Solution {
List<List<Integer>> retlist=new ArrayList<>(256);
ArrayList<Integer> arraylist;
public List<List<Integer>> subsetsWithDup(int[] nums)
{
int len=nums.length;
arraylist=new ArrayList<>(512);
Arrays.sort(nums);
for(int i=0;i<=len;i++)
dfs(i, 0, -1, nums);
return retlist;
}
public void dfs(int target,int deep,int lastindex,int[] nums)
{
if(deep==target)
{
retlist.add(new ArrayList<>(arraylist));
return ;
}
for(int i=lastindex+1;i<nums.length;i++)
{
if(i>0&&nums[i]==nums[i-1])
if(lastindex!=i-1)
continue;
arraylist.add(nums[i]);
dfs(target, deep+1, i, nums);
arraylist.remove(arraylist.size()-1);
}
}
}
相关文章推荐
- Windows平台下fwrite函数0x0A变成0x0D 0x0A
- Ubuntu下如何安装TensorFlow
- 自定义View——旋转小球Loadding
- 关于eof的烦恼
- ubuntu echo输出带颜色的字体
- GO语言按照深度遍历文件
- html5 canvas图像在谷歌浏览器下不显示问题
- Git- 服务搭建
- content-type
- linux apache mysql php
- 查看log的方法
- 关于网络编程中MTU、TCP、UDP优化配置的一些总结
- hdoj 4548 美素数《打表》
- 【FAQ】git merge 后 push 到 Gerrit 失败,提示 no new changes ?
- 217. Contains Duplicate
- 栈和队列面试题之--实现一个有getMin功能的栈
- postfixadmin忘记密码后的修改密码方法详解
- oraclede chuangjian yu dajian(zhuan)
- poj2049 Finding Nemo(优先队列BFS)
- 爬虫系统邮件发送设置