46. 全排列_中等_模拟

class Solution {

    void perm(int level, int []nums,  List<List<Integer>> list){
        if(level==nums.length){
            ArrayList<Integer> listSingle = new ArrayList<>();
            for(int i=0;i<nums.length;i++){
                listSingle.add(nums[i]);
            }
            list.add(listSingle);
        }else{
            for(int i=level;i<nums.length;i++){
                int temp = nums[i];
                nums[i] = nums[level];
                nums[level] = temp;
                perm(level+1,nums,list);
                temp = nums[i];
                nums[i] = nums[level];
                nums[level] = temp;
            }
        }
    }

    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> listAll = new ArrayList<>();
        perm(0,nums,listAll);
        return listAll;
    }
}
作者:你的雷哥
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/henuliulei/p/15354872.html