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

[LeetCode]Subsets II

时间:2014-11-19 01:33:20      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:java   leetcode   

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 {
	List<List<Integer>> res = new ArrayList<List<Integer>>();
	List<Integer> list = new ArrayList<Integer>();
    public List<List<Integer>> subsetsWithDup(int[] num) {
    	Arrays.sort(num);
        help(num,0);
        return res;
    }

    private  void help(int[]num,int start){
    	if(!res.contains(list)){
    		List<Integer> lin = new ArrayList<Integer>(list);
    	    res.add(lin);
    	}
    	for(int i=start;i<num.length;i++){
    		list.add(num[i]);
    		help(num,i+1);
    		list.remove(Integer.valueOf((num[i])));
    	}
    }
}




[LeetCode]Subsets II

标签:java   leetcode   

原文地址:http://blog.csdn.net/guorudi/article/details/41253263

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