首页 > 代码库 > Subsets II
Subsets II
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], [] ]
public class Solution { public List<List<Integer>> subsetsWithDup(int[] num) { Arrays.sort(num); List<List<Integer>> result=new LinkedList<List<Integer>>(); LinkedList<Integer> subset=new LinkedList<Integer>(); if(num==null) return result; result.add(subset); getSubset(num,0,subset,result); HashSet set=new HashSet(result); result.clear(); result.addAll(set); return result; } public void getSubset(int []num,int begin,LinkedList<Integer> subset,List<List<Integer>> result){ if(begin==num.length){ return; } for(int i=begin;i<num.length;i++){ subset.add(num[i]); LinkedList<Integer> list=new LinkedList<Integer>(); list.addAll(subset); result.add(list); getSubset(num,i+1,subset,result); subset.pollLast(); } } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。