美文网首页
46. Permutations

46. Permutations

作者: 衣介书生 | 来源:发表于2018-04-15 22:40 被阅读11次

题目分析

回溯法,这道题理解的不够好,特此标记,后补。

代码

class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        if(nums == null || nums.length == 0) return res;
        helper(res, new ArrayList<>(), nums);
        return res;
    }
    public static void helper(List<List<Integer>> res, List<Integer> list, int[] nums) {
        if(list.size() == nums.length) {
            res.add(new ArrayList<Integer>(list));
        }
        for(int i = 0; i < nums.length; i++) {
            if(list.contains(nums[i])) continue;
            list.add(nums[i]);
            helper(res, list, nums);
            list.remove(list.size() - 1);
        }
    }
}

相关文章

网友评论

      本文标题:46. Permutations

      本文链接:https://www.haomeiwen.com/subject/pgijkftx.html