首页 > 其他 > 详细

Subsets II

时间:2014-02-13 14:55:09      阅读:369      评论:0      收藏:0      [点我收藏+]

Given a collection of integers that might contain duplicates, S, return all possible subsets.

If S = [1,2,2], a solution is:

[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]
bubuko.com,布布扣
 1 public class Solution{
 2 public ArrayList<ArrayList<Integer>> subsetsWithDup(int[] num) {
 3         Arrays.sort(num);
 4         ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
 5         res.add(new ArrayList<Integer>());
 6         if(num.length<=0) return res;
 7         DFS(num,0,new ArrayList<Integer>(),res);
 8         return res;
 9     }
10     public void DFS(int []num,int start,ArrayList<Integer>output,ArrayList<ArrayList<Integer>>res ){
11         for(int i=start;i<num.length;i++){
12             ArrayList<Integer>temp = new ArrayList<Integer>();
13             output.add(num[i]);
14             temp.addAll(output);
15             res.add(temp);
16             DFS(num,i+1,output,res);
17             output.remove(output.size()-1);
18             while(i<num.length-1 && num[i]==num[i+1]){
19                 i++;
20             }
21         }
22     }
23 }
View Code

Subsets II

原文:http://www.cnblogs.com/krunning/p/3547427.html

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