第七章 回溯算法part03 39. 组合总和 40. 组合总和II 131. 分割回文串

第二十七天| 第七章 回溯算法part03 39. 组合总和 40. 组合总和II 131. 分割回文串

一、39. 组合总和

一、39. 组合总和

  • 题目链接:https://leetcode.cn/problems/combination-sum/

  • 题目介绍:

    • 给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
    • candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。
    • 对于给定的输入,保证和为 target 的不同组合数少于 150 个。
  • 思路:

    • 同一个集合所以用startIndex
    • 又因为是可以重复,所以递归传参是startIndex
    • 这里的剪枝逻辑是:如果当前的sum + candidates[i] > target就可以剪枝了
  • 代码:

class Solution {
    LinkedList<Integer> path = new LinkedList<>();
    List<List<Integer>> result = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        backtracking(candidates, target, 0, 0);
        return result;
    }

    public void backtracking(int[] candidates, int target, int sum, int startIndex) {
        if (sum == target) {
            result.add(new ArrayList<>(path));
            return;
        }
        if (sum > target) {
            return;
        }
        for (int i = startIndex; i < candidates.length; i++) {
            sum += candidates[i];
            path.add(candidates[i]);
            // 为什么每次递归起始下标都是i呢?
            // 为什么每次不是从0开始呢?
            // 这么想:下标组合1、1、2和1、2、1是重复的,回溯到第二层的时候之后的下标只能从2开始,所以后者1、2、...,后面这些数的其实下标必须为2,即当前i
            backtracking(candidates, target, sum, i);
            sum -= candidates[i];
            path.remove(path.size() - 1);
        }
        return;
    }
}

二、40. 组合总和II

  • 题目链接:https://leetcode.cn/problems/combination-sum-ii/description/

  • 题目描述:

    • 给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
    • candidates 中的每个数字在每个组合中只能使用 一次
    • **注意:**解集不能包含重复的组合。
  • 思路:

    • 本题的难点在于:数组中的元素是可重复的,要求解集中不能包含重复的组合
    • 所以:需要去重
      • 这里去重的逻辑类似于三数之和、四数之和
      • 首先需要对数组进行排序,因为排序之后,相等的值就会挨在一起,这样可以方便去重
        • 在回溯问题中,需要清楚是“树枝去重”还是“树层去重”,通过下图来解释一下这个问题
        • 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
        • 简单来说,就是你在同一个path中可以有重复的数值,但是你记录新的path时,如果元素和它的前一个元素值相等,说明这个值的情况已经遍历过了,那么就要去重。
        • 去重要定位在一个集合中(同一个循环中)
  • 代码:

class Solution {
    LinkedList<Integer> path = new LinkedList<>();
    List<List<Integer>> result = new ArrayList<>();
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        // 去重之前需要排序
        // 为的是便于去重
        Arrays.sort(candidates);
        backtracking(candidates, target, 0, 0);
        return result;
    }

    public void backtracking(int[] candidates, int target, int sum, int startIndex) {
        // 1. 递归结束的条件
        if (sum == target) {
            result.add(new ArrayList<>(path));
            return;
        }
        // 2. 单层递归的逻辑
        //  2.1 单层循环包括剪枝条件
        for (int i = startIndex; i < candidates.length && sum + candidates[i] <= target; i++) {
            //  2.2 在集合中进行去重操作(树层去重)
            if (i > startIndex && candidates[i - 1] == candidates[i]) {
                continue;
            }
            //  2.3 操作节点
            sum += candidates[i];
            path.add(candidates[i]);
            //  2.4 递归
            backtracking(candidates, target, sum, i + 1);
            //  2.5 回溯
            sum -= candidates[i];
            path.remove(path.size() - 1);
        }
        return;
    }
}
  • 注意:去重一定要排序

三、总结四道组合题目之间的异同

题目描述返回值一个集合中求组合(需要startIndex)
组合[1, n],k个数的组合每种组合中的元素不允许重复递归传参:i+ 1
组合总和Icandidates数组(无重复)中所有可以使数字和为 target 的组合数组中的元素可以无限制重复被选取递归传参:i
组合总和IIcandidates数组(有重复)中所有可以使数字和为 target 的组合数组中的每个数字在每个组合中只能使用一次递归传参:i + 1
组合总和III[1, 9],和为n的k个数组合每种组合中的元素不允许重复递归传参:i + 1

如果是一个集合来求组合的话,就需要startIndex,例如:4个组合问题

如果是多个集合取组合,各个集合之间相互不影响,那么就不用startIndex,例如:电话号码的字母组合

三、131. 分割回文串

  • 题目链接:https://leetcode.cn/problems/palindrome-partitioning/
  • 题目介绍:
    • 给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。
    • 回文串 是正着读和反着读都一样的字符串。
  • 思路:
    • 通过startIndex来切割子串
    • 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
  • 代码:
class Solution {
    List<List<String>> result = new ArrayList<>();
    LinkedList<String> path = new LinkedList<>();
    public List<List<String>> partition(String s) {
        backtracking(s, 0);
        return result;
    }

    // 1. 确定返回值和参数
    //  1.1 返回值:void
    //  1.2 参数:String s 和 用来切割的int startIndex
    public void backtracking(String s, int startIndex) {
        // 2. 确定终止条件
        //  2.1 因为startIndex是用来切割的,所以当startIndex >= s.length()时,说明达到了叶子节点
        if (startIndex >= s.length()) {
            result.add(new ArrayList<>(path));
            return;
        }
        // 3. 确定单层搜索的逻辑
        for (int i = startIndex; i < s.length(); i++) {
            //  3.1  当前循环的[startIndex, i]为切割下来的一段子字符串
            //  判断该子串是否是回文
            if (isPartition(s, startIndex, i)) {
                // substring这个函数不包含右边界点,所以需要 + 1
                String temp = s.substring(startIndex, i + 1);
                path.add(temp);
            } else {
                continue;
            }
            //  3.2 递归
            backtracking(s, i + 1);
            //  3.3 回溯
            path.remove(path.size() - 1);
        }
        return;
    }

    // 判断回文的函数
    public boolean isPartition(String s, int start, int end) {
        for (int i = start, j = end; i < j; i++, j--) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
        }
        return true;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值