码迷,mamicode.com
首页 > 其他好文 > 详细

Subsets II

时间:2014-08-14 20:42:59      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:数据结构   算法   

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();
        }
    }
}

Subsets II,布布扣,bubuko.com

Subsets II

标签:数据结构   算法   

原文地址:http://blog.csdn.net/dutsoft/article/details/38562193

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!