首页 > 其他 > 详细

Subsets

时间:2015-04-16 13:44:36      阅读:147      评论:0      收藏:0      [点我收藏+]

这道题虽然不用到helper,但是却是那一路的

参考code 包括有helper的 http://blog.csdn.net/linhuanmars/article/details/24286377

public class Solution {
    public ArrayList<ArrayList<Integer>> subsets(int[] S) {
        ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
        if(S==null || S.length==0) return res;
        Arrays.sort(S); //!!!
        ArrayList<Integer> item = new ArrayList<Integer>();
        res.add(item);
        for(int i=0; i<S.length;i++){
            int len = res.size(); // to accelerate 
            for(int j=0;j<len;j++){// 通过控制res size,保证不出现2,3 & 3,2
                ArrayList<Integer> t = new ArrayList<Integer>(res.get(j)); // 这里一定要new 一个新的array list,否则就会指回去 AL .. t = res.get(j)
                t.add(S[i]);
                res.add(t);
            }
        }
        return res;
    }
}

 

Subsets

原文:http://www.cnblogs.com/jiajiaxingxing/p/4431651.html

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