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;
    }
}
相关推荐
2501_924889552 小时前
商超高峰客流统计误差↓75%!陌讯多模态融合算法在智慧零售的实战解析
大数据·人工智能·算法·计算机视觉·零售
jingfeng5143 小时前
C++模板进阶
java·c++·算法
地平线开发者3 小时前
征程 6X | 常用工具介绍
算法·自动驾驶
地平线开发者3 小时前
理想汽车智驾方案介绍 2|MindVLA 方案详解
算法·自动驾驶
艾莉丝努力练剑4 小时前
【C语言16天强化训练】从基础入门到进阶:Day 7
java·c语言·学习·算法
地平线开发者4 小时前
LLM 中评价指标与训练概要介绍
算法·自动驾驶
Ghost-Face5 小时前
关于并查集
算法
flashlight_hi6 小时前
LeetCode 分类刷题:2529. 正整数和负整数的最大计数
python·算法·leetcode
花火|6 小时前
算法训练营day60 图论⑩ Bellman_ford 队列优化算法、判断负权回路、单源有限最短路
算法·图论
2501_924890526 小时前
商超场景徘徊识别误报率↓79%!陌讯多模态时序融合算法落地优化
java·大数据·人工智能·深度学习·算法·目标检测·计算机视觉