5/30/2014

65. Subsets

public class Solution {
    public ArrayList<ArrayList<Integer>> subsets(int[] S) {
        ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>> ();
        Arrays.sort(S);
        int length = S.length;
       
        for (int i=0; i<Math.pow(2, length); i++) {
            ArrayList<Integer> temp = new ArrayList<Integer> ();
            int t = i;
           
            for (int j=0; j<length; j++) {
                int bit = t&1;
                if (bit==1) temp.add(S[j]);
                t = t>>1;
            }
            res.add(temp);
        }
       
        return res;
    }
}

没有评论:

发表评论