码迷,mamicode.com
首页 > 编程语言 > 详细

排列组合算法

时间:2017-05-17 16:06:32      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:number   solution   code   comm   排列组合   span   com   []   cti   

public class Solution{
    public List<List<Integer>> permutations(int[] arr){
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        //corner
        if ( arr == null || arr.length == 0 ){
            return res;
        }

        //core
        List<Integer> curList = new ArrayList<>();

        helper(res, curList, arr);

        return res;
    }


    public void helper(List<List<Integer>> res, List<Integer> curList, int[] arr){
        //base
        if ( curList.size() == arr.length ){
            res.add(new ArrayList<Integer>(curList));           
            return ;
        }

        //current
        for ( int i = 0; i < arr.length; i++ ){
            if ( curList.contains(arr[i]) ){
                continue;
            }

            curList.add(arr[i]);

            //next
            helper(res, curList, arr);

            curList.remove(curList.size() - 1);                   
        }
    }
}

排列组合算法

标签:number   solution   code   comm   排列组合   span   com   []   cti   

原文地址:http://www.cnblogs.com/energy1010/p/6867799.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!