算法训练(leetcode)二刷第十九天 | *39. 组合总和、*40. 组合总和 II、*131. 分割回文串

刷题记录

  • [*39. 组合总和](#*39. 组合总和)
  • [*40. 组合总和 II](#*40. 组合总和 II)
  • [*131. 分割回文串](#*131. 分割回文串)

*39. 组合总和

leetcode题目地址

元素可以重复,但结果不可以重复,此题与216. 组合总和 III的思路相似,只是,216. 组合总和 III不可重复使用数字。

不可重复使用,则在回溯算法下一次递归时就从下一个位置开始计算。

可以重复使用,则在回溯算法下一次递归时仍然从当前位置开始计算。

时间复杂度: O ( n ∗ 2 n ) O(n*2^n) O(n∗2n)
空间复杂度: O ( n ) O(n) O(n)

java 复制代码
// java
class Solution {

    private int sum = 0;
    private List<Integer> path = new LinkedList<>();
    private List<List<Integer>> result = new ArrayList<>();

    public void backtracing(int[] candidates, int target, int startIdx){
        if(startIdx >= candidates.length) return;
        if(sum > target) return;
        if(sum == target){
            result.add(new ArrayList<>(path));
            return;
        }
        
        for(int i=startIdx; i<candidates.length; i++){
            // 剪枝
            if(sum>=target) return;
            path.add(candidates[i]);
            sum += candidates[i];
            backtracing(candidates, target, i);
            sum -= candidates[i];
            path.removeLast();
        }
    }
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        // 排序
        Arrays.sort(candidates);
        backtracing(candidates, target, 0);
        return result;
    }
}

*40. 组合总和 II

leetcode题目地址

本题难点在于去重。数组中有重复数字,但不可以有重复组合。

因此需要先对数组排序,排序后在同一层递归中相同的数字只进行一次。

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( n ) O(n) O(n)

java 复制代码
// java
class Solution {

    private int sum = 0;
    private List<Integer> path = new LinkedList<>();
    private List<List<Integer>> result = new ArrayList<>();

    public void backtracking(int[] candidates, int target, int startIdx){
        if(sum > target) return;
        if(sum == target){
            result.add(new ArrayList<>(path));
            return;
        }

        for(int i=startIdx; i<candidates.length; i++){
            // 去重 本层循环已使用相同元素
            if(i>startIdx && candidates[i] == candidates[i-1]) continue;
            if(sum >= target) return;
            sum += candidates[i];
            path.add(candidates[i]);
            backtracking(candidates, target, i+1);
            sum -= candidates[i];
            path.removeLast();
        }
    }

    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        backtracking(candidates, target, 0);
        return result;
    }
}

*131. 分割回文串

leetcode题目地址

这里需要注意记录字符串每次递归都需要新创建一个空串,而不是接着上一层接着拼接。

时间复杂度: O ( n ∗ 2 n ) O(n*2^n) O(n∗2n)
空间复杂度: O ( n 2 ) O(n^2) O(n2)

java 复制代码
// java
class Solution {
    private List<String> path = new LinkedList<>();
    private List<List<String>> result = new ArrayList<>();


    public boolean isPalindrome(StringBuilder s){
        for(int i=0; i<=s.length()/2; i++){
            if(s.charAt(i) != s.charAt(s.length()-1-i)){
                return false;
            }
        }
        return true;
    }
    public void backtracing(String s, int startIdx, StringBuilder sb){
        if(startIdx >= s.length()) {
            result.add(new ArrayList<>(path));
            return;
        }
        
        for(int i=startIdx; i<s.length(); i++){
            sb.append(s.charAt(i));    
            if(isPalindrome(sb)){
                path.add(sb.toString());
                backtracing(s, i+1, sb);
                path.removeLast();
            }
        }
    }

    public List<List<String>> partition(String s) {
        backtracing(s, 0, new StringBuilder());
        return result;
    }
}
相关推荐
王景程4 分钟前
让IOT版说话
后端·python·flask
JJJJ_iii8 分钟前
【机器学习11】决策树进阶、随机森林、XGBoost、模型对比
人工智能·python·神经网络·算法·决策树·随机森林·机器学习
Eiceblue26 分钟前
使用 Python 向 PDF 添加附件与附件注释
linux·开发语言·vscode·python·pdf
咚咚王者37 分钟前
人工智能之编程基础 Python 入门:第五章 基本数据类型(一)
人工智能·python
别学LeetCode1 小时前
#leetcode#
leetcode
loong_XL1 小时前
AC自动机算法-字符串搜索算法:敏感词检测
开发语言·算法·c#
Xの哲學1 小时前
Linux Netlink全面解析:从原理到实践
linux·网络·算法·架构·边缘计算
Tisfy1 小时前
LeetCode 3289.数字小镇中的捣蛋鬼:哈希表O(n)空间 / 位运算O(1)空间
算法·leetcode·散列表·题解·位运算·哈希表
@LetsTGBot搜索引擎机器人2 小时前
从零打造 Telegram 中文生态:界面汉化 + 中文Bot + @letstgbot 搜索引擎整合实战
开发语言·python·搜索引擎·github·全文检索
2501_938963962 小时前
基于音乐推荐数据的逻辑回归实验报告:曲风特征与用户收听意愿预测
算法·机器学习·逻辑回归