Subsets

Subsets
 1 public class Solution {
 2     public ArrayList> subsets(int[] S) {
 3         // IMPORTANT: Please reset any member data you declared, as
 4         // the same Solution instance will be reused for each test case.
 5         ArrayList> result = new ArrayList>();
 6         ArrayList list = new ArrayList();
 7         Arrays.sort(S);
 8         generate(result, list, S, 0, S.length);
 9         return result;
10     }
11     
12     private void generate(ArrayList> result, ArrayList list, int[] S, int depth, int length)
13     {
14         result.add(list);
15         if(depth == length)
16             return;
17         for(int i = depth; i < length; i++)
18         {
19             ArrayList tmp = new ArrayList();
20             tmp.addAll(list);
21             tmp.add(S[i]);
22             generate(result, tmp, S, i+1, length);
23         }
24     }
25 }

 

posted on 2013-11-12 13:57  JasonChang 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/jasonC/p/3419394.html

你可能感兴趣的:(Subsets)