LeetCode 46. 全排列

LeetCode 46. 全排列_第1张图片

 

public static List> permute(int[] nums) {

        List> ll = new ArrayList<>();
        List l = new ArrayList<>();
        boolean [] visit = new boolean[nums.length];
        dfs(nums,ll,l,visit);
        return ll;
    }

    private static void dfs(int[] nums, List> ll, List l, boolean[] v) {
        if(l.size() == nums.length){
            ll.add(new ArrayList<>(l));
            return;
        }else {
            for (int j = 0; j < nums.length; j++) {
                if(v[j])continue;
                l.add(nums[j]);
                v[j] = true;
                dfs(nums,ll,l,v);
                v[j] = false;
                l.remove(l.size() -1);
            }
        }
    }

 

你可能感兴趣的:(算法,LeetCode,46.,全排列)