算法训练(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;
    }
}
相关推荐
点云-激光雷达-Slam-三维牙齿2 小时前
速度起飞 笔记本电脑6G显卡llama运行Qwen3.6 35BA3B MTP 大模型
人工智能·python·电脑·llama
言乐62 小时前
Python游戏水平测试辅助系统
开发语言·python·游戏·django·pygame
从零开始学习人工智能9 小时前
【踩坑实录】WSL2 解决 onnxruntime\-gpu ImportError: libcudart\.so\.13 无 CUDA13 运行库问题
python
大明者省9 小时前
WSL2 Ubuntu22.04 GPU训练环境配置指南
人工智能·算法·计算机视觉
卷无止境10 小时前
在 awesome-fastapi 里,哪些库值得一看?
后端·python
zhanghaha131410 小时前
Python进阶教程:6_JSON 数据解析 —— 新手完全指南
开发语言·python·json
Python私教10 小时前
API 输出模型怎么设计:从 model_dump() 到显式展示层
python·fastapi
白狐_79810 小时前
408数据结构第8章:排序②——性质对比秒杀、场景选择与外部排序
java·数据结构·算法
Python私教11 小时前
本地 AI 工具服务该绑定 127.0.0.1 还是 0.0.0.0?
python·fastapi
zander25811 小时前
LeetCode 84:柱状图中的最大矩形——单调栈如何确定左右边界
java·数据结构·算法