31打卡 排列型回溯

注意点

目前遇到的题目中,排列型问题主要考虑三点。

  1. 同一树枝上元素不能重复使用
  2. 数层是否需要去重
  3. 如果数层需要去重,那么要对nums进行排序

题目

46. 全排列

class Solution {
    List<List<Integer>> res= new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    int[] used;
    public List<List<Integer>> permute(int[] nums) {
        used = new int[nums.length];
        dfs(nums,used);
        return res;
    }
    /**
     * 子问题 从剩下的元素中构建集合
     * 当前操作 选取一个元素
     *
     * @param nums
     * @param used
     */
    public void dfs(int[] nums,int[] used){
        if (path.size() == nums.length) {
            res.add(new ArrayList<>(path));
        }
        for (int i = 0; i < nums.length; i++) {
            //树枝不重复
            if (used[i] != 1){
                path.add(nums[i]);
                used[i] = 1;
                dfs(nums,used);
                path.remove(path.size() - 1);
                used[i] = 0;
            }
        }
    }
}

47. 全排列 II

public class L0047 {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    int[] used;
    public List<List<Integer>> permuteUnique(int[] nums) {
        Arrays.sort(nums);

        used = new int[nums.length];
        dfs(nums, used);
        return res;
    }

    /**
     * 带去重的全排列  考虑两点
     * 1、 数层去重
     * 2、 树枝元素不重复使用
     */
    public void dfs(int[] nums, int[] used) {
        if (path.size() == nums.length) {
            res.add(new ArrayList<>(path));
        }

        for (int i = 0; i < nums.length; i++) {
            //数层去重,一定是发生在同一层的第一个树枝之后
            if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == 0) {
                continue;
            }
            //树枝不重复
            if (used[i] == 0) {

                path.add(nums[i]);
                used[i] = 1;
                dfs(nums, used);
                path.remove(path.size() - 1);
                used[i] = 0;
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值