day20 回溯算法part02

**问题1:**组合总和

题目:

39. 组合总和 - 力扣(LeetCode)

思路:

递归三部曲,注意这里的index 因为可以取两次

代码:

java 复制代码
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class 组合总和 {
    public static void main(String[] args) {

    }
    List<List<Integer>> res = new LinkedList<>();
    List<Integer> lhy       = new LinkedList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        trackbake(candidates,0,1,target);
        return res;
    }
    public void trackbake(int [] candidates,int sum,int index,int target){
        /*
        递归三部曲:
            参数和返回值: 数组 , 涉及到求和 那有经验了直接设参 以及起始位置及目标值
            终止条件:    幻想的叶子节点
            单层递归逻辑: 当和等于目标值的时候 添加
         */
        if (sum > target) return;
        if (sum == target){
            res.add(lhy);
            return;
        }
        for (int i = index; i < candidates.length; i++){
            if (sum > target) return;
            lhy.add(candidates[i]);
            trackbake(candidates,sum+ candidates[i],i,target);
            sum -= candidates[i];
        }
    }
}
**问题2:**组合总和II

题目:

40. 组合总和 II - 力扣(LeetCode)

思路:

这里我想到用hashmap 但是会有额外存储花销 可以优化

代码:

java 复制代码
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

public class 组合总和II {
    public static void main(String[] args) {

    }
    List<List<Integer>> res = new LinkedList<>();
    List<Integer> path = new LinkedList<>();
    HashMap<List<Integer>,Integer> tmp = new HashMap<>();
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        trackback(candidates,0,0,target);
        return res;
    }
    public void trackback(int [] candidate, int index, int sum, int target){
            if (sum == target){
                if (!tmp.containsKey(path)){
                tmp.put(path,tmp.getOrDefault(path,0)+1);
                res.add(new LinkedList<>(path));
                return;
            }}
        for (int i = index; i < candidate.length; i++){
            if (sum == target){
                return;
            }
            path.add(candidate[i]);
            trackback(candidate,i+1,sum+candidate[i],target);
            path.remove(path.size()-1);
        }
    }
}
**问题3:**分割回文串

题目:

131. 分割回文串 - 力扣(LeetCode)

思路:

借用StringBuilder去操作;其他和组合问题一样的;

代码:

java 复制代码
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.List;

public class 分割字符串 {
    public static void main(String[] args) {
        System.out.println(partition("aab"));
    }
    public static List<List<String>> partition(String s) {
            trackback(s,0,new StringBuilder());
            return res;
    }
    static List<List<String>> res = new ArrayList<>();
    static List<String> path = new ArrayList<>();
    public static void trackback(String s, int index,StringBuilder sb){
        if (!Palindrome(sb.toString())) return;
        if (s.length() == index){
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = index; i < s.length();i++){
            sb.append(s.charAt(i));
            if (Palindrome(sb.toString())){
                path.add(sb.toString());
                trackback(s,i+1,new StringBuilder());
                path.remove(path.size()-1);
            }

        }
    }
    public static boolean Palindrome(String s){
        int left = 0;
        int right=s.length()-1;
        while (left <= right){
            if (s.charAt(left) == s.charAt(right)){
                left++;
                right--;
            }else return false;
        }
        return true;
    }
}
相关推荐
嵌入式进阶行者8 小时前
【算法】TLV格式解析实例:华为OD机考双机位A卷 - TLV解析 Ⅱ
数据结构·c++·算法
OC溥哥9998 小时前
Paper MinecraftV3.0重大更新(下界更新)我的世界C++2D版本隆重推出,拷贝即玩!
java·c++·算法
Jayden_Ruan8 小时前
C++蛇形方阵
开发语言·c++·算法
星火开发设计8 小时前
C++ map 全面解析与实战指南
java·数据结构·c++·学习·算法·map·知识
执笔论英雄8 小时前
【RL] advantages白化与 GRPO中 advantages均值,怎么变化,
算法·均值算法
2301_800895108 小时前
hh的蓝桥杯每日一题
算法·职场和发展·蓝桥杯
老鱼说AI8 小时前
现代计算机系统1.2:程序的生命周期从 C/C++ 到 Rust
c语言·c++·算法
仰泳的熊猫8 小时前
题目1099:校门外的树
数据结构·c++·算法·蓝桥杯
求梦8209 小时前
【力扣hot100题】反转链表(18)
算法·leetcode·职场和发展