算法训练(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;
    }
}
相关推荐
冷雨夜中漫步5 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
郝学胜-神的一滴5 小时前
深入解析Python字典的继承关系:从abc模块看设计之美
网络·数据结构·python·程序人生
百锦再5 小时前
Reactive编程入门:Project Reactor 深度指南
前端·javascript·python·react.js·django·前端框架·reactjs
颜酱7 小时前
图结构完全解析:从基础概念到遍历实现
javascript·后端·算法
m0_736919107 小时前
C++代码风格检查工具
开发语言·c++·算法
yugi9878387 小时前
基于MATLAB强化学习的单智能体与多智能体路径规划算法
算法·matlab
喵手7 小时前
Python爬虫实战:旅游数据采集实战 - 携程&去哪儿酒店机票价格监控完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集结果csv导出·旅游数据采集·携程/去哪儿酒店机票价格监控
2501_944934737 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
helloworldandy7 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
DuHz7 小时前
超宽带脉冲无线电(Ultra Wideband Impulse Radio, UWB)简介
论文阅读·算法·汽车·信息与通信·信号处理