Subclasses

  Given a collection of numbers, return all possible subclasses.

 1 public class Solution {
 2     public List<List<Integer>> permute(int[] num) {
 3         ArrayList<List<Integer>> result = new ArrayList<List<Integer>>();
 4         ArrayList<Integer> row = null;
 5         if(num == null ||num.length == 0) return result;
 6         long size = (long)Math.pow(2, num.length);
 7         for(long i = 0; i < size; i ++){
 8             row = new ArrayList<Integer>();
 9             for(int j = 0; j < num.length; j ++){
10                 if((i >> j) % 2 == 1) row.add(num[j]);
11             }
12             result.add(row);
13         }
14         return result;
15     }
16 }

 

posted on 2014-08-19 09:05  Step-BY-Step  阅读(222)  评论(0编辑  收藏  举报

导航