leetcode-46

public class Solution46 {

    public void backTrack(int n, List<Integer> nums, List<List<Integer>> output, int first) {
        if (first == n) {
            output.add(new ArrayList<>(nums));
        }
        for (int i = first; i < n; i++) {
            Collections.swap(nums, first, i);
            backTrack(n, nums, output, first + 1);
            Collections.swap(nums, first, i);
        }
    }

    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> output = new LinkedList<>();
        List<Integer> nums_list = new ArrayList<>();
        for (int n : nums) {
            nums_list.add(n);
        }
        int n = nums_list.size();
        backTrack(n, nums_list, output, 0);
        return output;
    }
}

end

猜你喜欢

转载自www.cnblogs.com/CherryTab/p/12289442.html