LeetCode 46. Permutations

问题描述

  • Given a collection of distinct integers, return all possible permutations.
  • Example :

Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]

问题分析

  • 经典的全排列问题,找出所有排列的可能性。用 DFS + 回溯 的思想解决即可。
  • 但这题与其他 回溯题稍有不同的是,当对当前进行决策时,后续决策的可能选型受到了当前决策的影响。如第一步选择2,那么第2,3步肯定无法选择 2了。
    解决上述问题的方法有:
  • 设置一个 used[]数组,或者是 hashSet,来存储dfs过程中已经使用的数字。剪枝的思想
  • 第二个则是,将后续位置(决策)与当前位置数字进行交换,这样num数组就可以起到 path的作用,但这样其实会影响排列的顺序。如果对排列顺序有要求的话,这样是不对的。
    但上述两种方法都注意,在dfs之后,要进行回溯复原。used 相应位置变为 false, 或者从hashset中删除,保证used与hashset中始终维持的是一条路径的信息,方法二则需要重新交换回来。
    这里写图片描述
  • 该题也可以用 bfs ,但较为复杂

经验教训

  • 全排列问题,便是列举出所有的可能性,所以一定是用 DFS + 回溯 的思想

代码实现

  • DFS + 回溯(used数组)
    public List<List<Integer>> permute(int[] nums) {
        if (nums == null || nums.length == 0) {
            return new ArrayList<>();
        }
        List<List<Integer>> res = new ArrayList<>();
        //used[i] 用于标志 nums[i] 在当前路径是否使用过
        boolean[] used = new boolean[nums.length];
        findPermutations(nums, 0, new ArrayList<Integer>(), used, res);
        return res;
    }

    public void findPermutations(int[] nums, int i, ArrayList<Integer> path, boolean[] used, List<List<Integer>> res) {
        if (i == nums.length) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int j = 0; j < nums.length; ++j) {
            if (! used[j]) {
                path.add(nums[j]);
                used[j] = true;
                findPermutations(nums, i + 1, path, used, res);
                //回溯复原
                used[j] = false;
                path.remove(path.size() - 1);
            }
        }
        return;
    }
  • DFS + 回溯(交换)
    public List<List<Integer>> permute(int[] nums) {
        if (nums == null || nums.length == 0) {
            return new ArrayList<>();
        }
        List<List<Integer>> res = new ArrayList<>();
        findPermutations(nums, 0, res);
        return res;
    }

    //nums[0 ~ i - 1] 已经完成决策,现在对[i ~ len]进行决策,将最终结果添加进res
    public void findPermutations(int[] nums, int i, List<List<Integer>> res) {
        if (i == nums.length) {
            ArrayList<Integer> list = new ArrayList<>();
            for (int k = 0; k < nums.length; ++k) {
                list.add(nums[k]);
            }
            res.add(list);
        }
        //对i位置可能的情况进行枚举
        for (int j = i; j < nums.length; ++j) {
            //因为当前位置的确定,会影响后序决策,所以采用交换的方式
            //这样到了最后,nums[]也是记录的路径,不用额外的path记录了
            swap(nums, i, j);
            //继续 i+1 ~ len 的决策
            findPermutations(nums, i + 1, res);
            //利用回溯,复原数组。
            swap(nums, i ,j);
        }
        return;
    }

    public void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
  • BFS
    public List<List<Integer>> permute(int[] nums) {
        if (nums == null || nums.length == 0) {
            return new ArrayList<>();
        }
        List<List<Integer>> res = new ArrayList<>();
        LinkedList<List<Integer>> queue = new LinkedList<>();
        //初始化队列
        queue.add(new ArrayList<>());
        for (int i = 0; i < nums.length; i++) {
            int levelSize = queue.size();
            for (int count = 0; count < levelSize; ++count) {
                List<Integer> list = queue.remove();
                for (int j = 0; j < nums.length; j++) {
                    if (! list.contains(nums[j])) { //此处也可以用set判重
                        ArrayList<Integer> newList = new ArrayList<>(list);
                        newList.add(nums[j]);
                        queue.add(newList);
                    }
                }
            }
        }
        res.addAll(queue);
        return res;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值