算法训练(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;
    }
}
相关推荐
萧西待水9 小时前
奥赛一本通 1447 靶形数独
算法·深度优先
麻雀飞吧11 小时前
先判断工具用来学习、开发还是执行
人工智能·python
泯泷11 小时前
那段文字是谁删的?Yjs 14 正式版之前,一套删除归属方案的实现与边界
前端·javascript·算法
hold?fish:palm11 小时前
34 合并K个升序链表
javascript·算法·链表
人邮异步社区11 小时前
学习Python的最佳学习路径是什么?
python·程序员
Navigator_Z13 小时前
LeetCode //C - 1252. Cells with Odd Values in a Matrix
c语言·算法·leetcode
troy12813 小时前
Python 基础语法(九):Django/Flask/FastAPI 三大 Web 框架详细对比解析
python·jupyter·django·flask·github·fastapi
jzshmyt14 小时前
我用 Python 从零“生成“了一个宇宙,然后让它观察自己(v14)
人工智能·pytorch·python·numpy·matplotlib·空间计算·scipy
语戚15 小时前
力扣 1621. 大小为K的不重叠线段的数目:动态规划(Java 实现)
java·算法·leetcode·动态规划·力扣·dp
青山木15 小时前
Hot 100 --- 最长有效括号
java·数据结构·算法·leetcode·动态规划