【LeetCode】hot100—46. 全排列_回溯



题目

给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。

示例 1:

输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

示例 2:

输入:nums = [0,1]
输出:[[0,1],[1,0]]

示例 3:

输入:nums = [1]
输出:[[1]]

提示:

  • 1 <= nums.length <= 6
  • -10 <= nums[i] <= 10
  • nums 中的所有整数 互不相同

1- 思路

经典回溯题目
在回溯问题中,回溯的结果可以构造成一棵树的结构

  • 集合的大小就构成了——>树的宽度
  • 递归的深度构成——>树的深度。

本题目所求的结果为全排列,区分于 组合问题
组合问题

  • 组合问题每次需要在根节点的子树中移除一个已经参与组合的结点,例如集合 {1,2,3} ,寻找n=2的组合:
  • 第一个子树可选择的范围为 {1,2,3} 从1为起始点为 1 开始寻找;
  • 第二个子树可选择的范围为 {2,3} 由于 1 已经使用过此时 起始点为 2

其回溯过程图如下
image.png


排列问题

  • 排列的过程相当于 选择 n 个数的不同排列顺序,因此构造的回溯树,每层中可选择的序列的长度是相同的,对于输入的集合 {1,2,3}
  • 选择起始点 1 则第一棵子树 可选择的范围为 {2,3}
  • 选择起始点为 2 则第二棵子树 可选择的范围为 {1,3]
  • 选择起始点为 3 则第三棵子树 可选择的范围为 {1,2}

其回溯过程图如下:
image.png


2- 题解

⭐全排列 —— 代码实现思路

image.png

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    boolean[] used;
    public List<List<Integer>> permute(int[] nums) {
        used = new boolean[nums.length];
        Arrays.fill(used,false);
        backTracing(nums);
        return res;
    }

    public void backTracing(int[] nums){
        // 终止条件
        if(path.size()==nums.length){
            res.add(new ArrayList(path));
        } 

        // 单层
        for(int i = 0 ; i < nums.length;i++){
            if(used[i]==true){
                continue;
            }
            path.add(nums[i]);
            used[i] = true;
            backTracing(nums);
            used[i] = false;
            path.removeLast();
        }
    }
}
复杂度分析

image.png


3-ACM模式

public class hot54_permute {
    private static List<List<Integer>> res = new ArrayList<>();
    private static List<Integer> path = new ArrayList<>();
    private static boolean[] used;
    public static List<List<Integer>> permute(int[] nums) {
        used = new boolean[nums.length];
        Arrays.fill(used,false);
        backTracing(nums);
        return res;
    }

    public static void backTracing(int[] nums){
        // 终止条件
        if(path.size()==nums.length){
            res.add(new ArrayList(path));
        }

        // 单层
        for(int i = 0 ; i < nums.length;i++){
            if(used[i]==true){
                continue;
            }
            path.add(nums[i]);
            used[i] = true;
            backTracing(nums);
            used[i] = false;
            path.remove(path.size()-1);
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入数组长度n");
        int n = sc.nextInt();
        int[] nums = new int[n];
        System.out.println("输入数组");
        for (int i = 0; i < nums.length; i++) {
            nums[i] = sc.nextInt();
        }
        List<List<Integer>> forRes = permute(nums);

        for (List<Integer> a : forRes) {
            System.out.println(a);
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值