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;
    }
}
相关推荐
mit6.8245 小时前
bfs|栈
算法
CoderYanger6 小时前
优选算法-栈:67.基本计算器Ⅱ
java·开发语言·算法·leetcode·职场和发展·1024程序员节
jllllyuz6 小时前
Matlab实现基于Matrix Pencil算法实现声源信号角度和时间估计
开发语言·算法·matlab
夏鹏今天学习了吗7 小时前
【LeetCode热题100(72/100)】前 K 个高频元素
leetcode
稚辉君.MCA_P8_Java7 小时前
DeepSeek 插入排序
linux·后端·算法·架构·排序算法
多多*7 小时前
Java复习 操作系统原理 计算机网络相关 2025年11月23日
java·开发语言·网络·算法·spring·microsoft·maven
덕화7 小时前
【面试宝典】线上问题逆向分析1
面试·职场和发展
美团程序员7 小时前
一篇文章教你搞定:”xx 功能如何测试?“常见面试题型!
测试工具·面试·职场和发展·测试用例
.YM.Z8 小时前
【数据结构】:排序(一)
数据结构·算法·排序算法
Chat_zhanggong3458 小时前
K4A8G165WC-BITD产品推荐
人工智能·嵌入式硬件·算法