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

posted @ 2020-02-09 23:41  zhangyu63  阅读(153)  评论(0编辑  收藏  举报