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;
    }
}
相关推荐
NAGNIP11 小时前
一文搞懂深度学习中的通用逼近定理!
人工智能·算法·面试
颜酱20 小时前
单调栈:从模板到实战
javascript·后端·算法
CoovallyAIHub1 天前
仿生学突破:SILD模型如何让无人机在电力线迷宫中发现“隐形威胁”
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
从春晚机器人到零样本革命:YOLO26-Pose姿态估计实战指南
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
Le-DETR:省80%预训练数据,这个实时检测Transformer刷新SOTA|Georgia Tech & 北交大
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
强化学习凭什么比监督学习更聪明?RL的“聪明”并非来自算法,而是因为它学会了“挑食”
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
YOLO-IOD深度解析:打破实时增量目标检测的三重知识冲突
深度学习·算法·计算机视觉
NAGNIP1 天前
轻松搞懂全连接神经网络结构!
人工智能·算法·面试
NAGNIP1 天前
一文搞懂激活函数!
算法·面试
董董灿是个攻城狮1 天前
AI 视觉连载7:传统 CV 之高斯滤波实战
算法