90. Subsets II

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 = [1,2,2], a solution is:

[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]

此题以前做过类似的题目,不做解释,代码如下:
 1 public class Solution {
 2     public List<List<Integer>> subsetsWithDup(int[] nums) {
 3         List<List<Integer>> res= new ArrayList<>();
 4         Arrays.sort(nums);
 5         helper(res,new ArrayList<Integer>(),nums,0);
 6         return res;
 7     }
 8     public void helper(List<List<Integer>> res,List<Integer> list,int[] nums,int index){
 9         if(!res.contains(list)){
10             res.add(new ArrayList<Integer>(list));
11         }
12         for(int i=index;i<nums.length;i++){
13             list.add(nums[i]);
14             helper(res,list,nums,i+1);
15             list.remove(list.size()-1);
16         }
17         return;
18         
19     }
20 }

 

posted @ 2017-03-06 12:32  CodesKiller  阅读(101)  评论(0编辑  收藏  举报