Permutations

题目
Given a collection of distinct numbers, return all possible permutations.

答案

class Solution {
    public List> permute(int[] nums) {
        List> list = new ArrayList>();
        List curr = new ArrayList();
        boolean[] used = new boolean[nums.length];
        recur(nums, used, 0, curr, list);
        return list;
    }
    
    private void recur(int[] nums, boolean[] used, int i, List curr, List> list) {
        if(i == nums.length) {
            list.add(new ArrayList(curr));
            return;
        }
        
        for(int j = 0; j < nums.length; j++) {
            if(!used[j]) {
                curr.add(nums[j]);
                used[j] = true;
                recur(nums, used, i + 1, curr, list);
                used[j] = false;
                curr.remove(curr.size() - 1);                
            }
        }
    }
}

你可能感兴趣的:(Permutations)