首页 > 其他 > 详细

90. Subsets II

时间:2017-03-06 14:31:49      阅读:237      评论:0      收藏:0      [点我收藏+]

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 }

 

90. Subsets II

原文:http://www.cnblogs.com/codeskiller/p/6509397.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!