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;
    }
}

 

相关文章:

  • 2021-08-09
  • 2021-07-23
  • 2021-07-22
  • 2021-06-24
  • 2021-06-02
  • 2021-08-26
猜你喜欢
  • 2021-05-20
  • 2021-08-05
  • 2021-10-02
  • 2021-12-30
相关资源
相似解决方案