day36——回溯专题

本文探讨了如何使用递归和回溯算法解决编程中的排列问题,包括全排列(解法一和解法二)以及全排列II。在全排列问题中,通过标记已使用的数字避免重复,而全排列II则引入了树层去重的策略。代码示例展示了如何实现这些算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


11. 递增子序列

题目链接:491. 递增子序列 - 力扣(LeetCode)

思路:递归回溯枚举所有组合:

  • 注意去重(由于不能排序,那么我们用之前的去重条件剪枝,只能达到部分去重的目的,结果集仍然可能存在重复集合,如1 3 4 1 1),当枚举到这两个1时,结果集[1, 1]还是会重复的,为了达到去重的目的,我们可以用一个Set集合来记录结果集。
  • 要保证选择的序列是递增的

回溯三部曲

  • 递归函数参数
public void dfs(int[] nums,int start){
}
  • 终止条件
   if(path.size()>=2){
            res.add(new ArrayList<>(path));
        }
  • 单层搜索逻辑

利用HashSet集合记录,避免重复,保证唯一。

image-20221123195122667

   for(int i = start;i < n;i++){
            if(i>start&&nums[i]==nums[i-1]) continue;
            if(path.size()>0&&nums[i]<path.get(path.size()-1)) continue;
            path.add(nums[i]);
            dfs(nums,i+1);
            path.remove(path.size()-1);
            }

Code

class Solution {
    HashSet<List<Integer>> res = new HashSet<>();
    List<Integer> path = new ArrayList<>();
    int n;
    public List<List<Integer>> findSubsequences(int[] nums) {
        n = nums.length;
        dfs(nums,0);
        return new ArrayList<List<Integer>>(res);
    }
    public void dfs(int[] nums,int start){
        if(path.size()>=2){
            res.add(new ArrayList<>(path));
        }
        for(int i = start;i < n;i++){
            if(i>start&&nums[i]==nums[i-1]) continue;
            if(path.size()>0&&nums[i]<path.get(path.size()-1)) continue;
            path.add(nums[i]);
            dfs(nums,i+1);
            path.remove(path.size()-1);
            }
        }
    }

排列问题

12.全排列

力扣题目链接

思路:dfs实现排列型枚举

解法一:

回溯三部曲:

  • 递归函数参数
   private void dfs(int[] nums){
   }
  • 递归终止条件

image-20221123200531826

    if (path.size() == nums.length){
            result.add(new ArrayList<>(path));
            return;
        }
  • 单层搜索的逻辑
 for (int i = 0; i < nums.length; i++){
            if (used[i]){
                continue;
            }
            used[i] = true;
            path.add(nums[i]);
            permuteHelper(nums);
            path.removeLast();
            used[i] = false;
        }

Code

class Solution {

    List<List<Integer>> result = new ArrayList<>();// 存放符合条件结果的集合
    LinkedList<Integer> path = new LinkedList<>();// 用来存放符合条件结果
    boolean[] used;
    public List<List<Integer>> permute(int[] nums) {
        if (nums.length == 0){
            return result;
        }
        used = new boolean[nums.length];
        dfs(nums);
        return result;
    }

    private void dfs(int[] nums){
        if (path.size() == nums.length){
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++){
            if (used[i]){
                continue;
            }
            used[i] = true;
            path.add(nums[i]);
            dfs(nums);
            path.removeLast();
            used[i] = false;
        }
    }
}
解法二:
// 解法2:通过判断path中是否存在数字,排除已经选择的数字
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> permute(int[] nums) {
        if (nums.length == 0) return result;
        dfs(nums, path);
        return result;
    }
    public void dfs(int[] nums, LinkedList<Integer> path) {
        if (path.size() == nums.length) {
            result.add(new ArrayList<>(path));
        }
        for (int i =0; i < nums.length; i++) {
            // 如果path中已有,则跳过
            if (path.contains(nums[i])) {
                continue;
            } 
            path.add(nums[i]);
            dfs(nums, path);
            path.removeLast();
        }
    }
}

13.全排列 II

力扣题目链接

image-20221123210735456

思路:本题需要去重,来保证不取重复元素,我们选择树层去重。

回溯三部曲:

  • 递归函数参数
 private void dfs(int[] nums, boolean[] used) {
 }
  • 递归终止条件
 if (path.size() == nums.length) {
            result.add(new ArrayList<>(path));
            return;
        }
  • 单层搜索的逻辑
 for (int i = 0; i < nums.length; i++) {
            // used[i - 1] == true,说明同⼀树⽀nums[i - 1]使⽤过
            // used[i - 1] == false,说明同⼀树层nums[i - 1]使⽤过
            // 如果同⼀树层nums[i - 1]使⽤过则直接跳过
            if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) {
                continue;
            }
            //如果同⼀树⽀nums[i]没使⽤过开始处理
            if (used[i] == false) {
                used[i] = true;//标记同⼀树⽀nums[i]使⽤过,防止同一树枝重复使用
                path.add(nums[i]);
                dfs(nums, used);
                path.remove(path.size() - 1);//回溯,说明同⼀树层nums[i]使⽤过,防止下一树层重复
                used[i] = false;//回溯
            }

Code:

class Solution {
    //存放结果
    List<List<Integer>> result = new ArrayList<>();
    //暂存结果
    List<Integer> path = new ArrayList<>();

    public List<List<Integer>> permuteUnique(int[] nums) {
        boolean[] used = new boolean[nums.length];
        Arrays.fill(used, false);
        Arrays.sort(nums);
        dfs(nums, used);
        return result;
    }

    private void dfs(int[] nums, boolean[] used) {
        if (path.size() == nums.length) {
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            // used[i - 1] == true,说明同⼀树⽀nums[i - 1]使⽤过
            // used[i - 1] == false,说明同⼀树层nums[i - 1]使⽤过
            // 如果同⼀树层nums[i - 1]使⽤过则直接跳过
            if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) {
                continue;
            }
            //如果同⼀树⽀nums[i]没使⽤过开始处理
            if (used[i] == false) {
                used[i] = true;//标记同⼀树⽀nums[i]使⽤过,防止同一树枝重复使用
                path.add(nums[i]);
                dfs(nums, used);
                path.remove(path.size() - 1);//回溯,说明同⼀树层nums[i]使⽤过,防止下一树层重复
                used[i] = false;//回溯
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值