23th Day| 39.组合总和,40.组合总和II,131.分割回文串

LeetCode 39 组合总和

题目链接:39.组合总和

java 复制代码
//剪枝优化
class Solution {
    List<Integer> path = new ArrayList<>();
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        Arrays.sort(candidates);
        backtracking(candidates, target,0, 0);
        return res;
    }

    public void backtracking(int[] nums, int target, int sum, int startIndex){
        if(sum == target){
            res.add(new ArrayList(path));
            return;
        }
        for(int i = startIndex; i < nums.length; i++){
            if(sum+nums[i] > target) break;
            path.add(nums[i]);
            backtracking(nums, target,sum+nums[i], i);//取得是当前元素
            path.removeLast();
        }
    }
}

LeetCode 40 组合总和II

题目链接:40.组合总和II

java 复制代码
class Solution {
    List<Integer> path = new ArrayList<>();
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        backtracking(candidates, target, 0, 0);
        return res;
    }

    public void backtracking(int[] nums, int target, int sum, int startIndex){
        if(sum == target){
            res.add(new ArrayList(path));
            return;
        }
        for(int i = startIndex; i < nums.length; i++){
            if(sum+nums[i] > target) break;
            if(i != startIndex && nums[i-1] == nums[i]) continue;//i > startIndex
            path.add(nums[i]);
            backtracking(nums, target, sum+nums[i], i+1);
            path.removeLast();
        }
    }
}

LeetCode 131 分割回文串

题目链接:131.分割回文串

java 复制代码
class Solution {
    LinkedList<String> path = new LinkedList<>();
    List<List<String>> res = new ArrayList<>();
    public List<List<String>> partition(String s) {
        backtracking(s, 0 , new StringBuilder());
        return res;
    }
    //每一个for都需要一个新的StringBuilder;
    public void backtracking(String s, int startIndex, StringBuilder sb){
        if(startIndex == s.length()){
            res.add(new ArrayList(path));
            return;
        }
        for(int i = startIndex; i < s.length(); i++){
            sb.append(s.charAt(i));
            if(check(sb)){
                path.add(sb.toString());
                backtracking(s, i+1, new StringBuilder());
                path.removeLast();
            }else{
                continue;
            }
        }
    }

    public boolean check(StringBuilder sb){
        int i = 0;
        int j = sb.length() -1;
        while(i < j){
            if(sb.charAt(i++) != sb.charAt(j--)) return false;
        }
        return true;
    }
}
相关推荐
阿Y加油吧14 分钟前
两道位运算 / 摩尔投票经典题复盘:只出现一次的数字 & 多数元素
数据结构·算法·leetcode
Evand J19 分钟前
【课题推荐】三模型IMM交互式多模型滤波算法,匀速/左转/右转目标跟踪,附MATLAB代码测试结果
算法·matlab·目标跟踪·无人机·imm·多模型
05候补工程师42 分钟前
【408狂飙·数据结构】核心考点深度复盘:数组地址计算、特殊矩阵压缩存储与树的五大性质解题直觉
数据结构·笔记·线性代数·考研·算法·矩阵
青山师1 小时前
HashMap深度解析:哈希冲突、扩容机制与线程安全
算法·安全·哈希算法·java面试·hashmap源码
货拉拉技术1 小时前
私域转化率翻倍的秘密:我们把多模态Agent融进了私域营销
人工智能·算法·设计模式
WL_Aurora1 小时前
备战蓝桥杯国赛【Day 17】
算法·蓝桥杯
生物信息与育种1 小时前
PlantBiMoE开源:轻量高效的植物基因组基础模型
人工智能·深度学习·职场和发展·数据分析·r语言
kcuwu.1 小时前
决策树与集成学习深度解析:从原理到实践
算法·决策树·集成学习
programhelp_2 小时前
2026 Fall Coinbase Software Engineer OA 真题分享与通关指南
算法
CQU_JIAKE2 小时前
5.19【A】
算法