算法训练(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;
    }
}
相关推荐
冬天给予的预感2 小时前
DAY 54 Inception网络及其思考
网络·python·深度学习
钢铁男儿2 小时前
PyQt5高级界而控件(容器:装载更多的控件QDockWidget)
数据库·python·qt
倔强的小石头_3 小时前
【C语言指南】函数指针深度解析
java·c语言·算法
Yasin Chen3 小时前
C# Dictionary源码分析
算法·unity·哈希算法
_Coin_-4 小时前
算法训练营DAY27 第八章 贪心算法 part01
算法·贪心算法
亿牛云爬虫专家6 小时前
Kubernetes下的分布式采集系统设计与实战:趋势监测失效引发的架构进化
分布式·python·架构·kubernetes·爬虫代理·监测·采集
董董灿是个攻城狮8 小时前
5分钟搞懂什么是窗口注意力?
算法
Dann Hiroaki8 小时前
笔记分享: 哈尔滨工业大学CS31002编译原理——02. 语法分析
笔记·算法
蹦蹦跳跳真可爱58910 小时前
Python----OpenCV(图像増强——高通滤波(索贝尔算子、沙尔算子、拉普拉斯算子),图像浮雕与特效处理)
人工智能·python·opencv·计算机视觉
nananaij10 小时前
【Python进阶篇 面向对象程序设计(3) 继承】
开发语言·python·神经网络·pycharm