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;
    }
}
相关推荐
量化君也1 分钟前
桥水基金全天候策略拆解,构建中国ETF躺平版策略
大数据·人工智能·python·算法·金融·业界资讯
蓦然回首却已人去楼空9 分钟前
画图专用文档
算法
洛水水11 分钟前
【力扣100题】78.在排序数组中查找元素的第一个和最后一个位置
数据结构·算法·leetcode
江屿风12 分钟前
C++图论基础拓扑排序算法流食般投喂
开发语言·c++·笔记·算法·排序算法
海棠AI实验室20 分钟前
AI 时代文献综述:从检索到成稿的 RAG 五步法
windows·算法·自动化·llm·rag
H1785350909620 分钟前
SolidWorks_基于草图的实体特征14_扫描扭转与控制
前端·人工智能·算法·3d建模·solidworks
黄金龙PLUS22 分钟前
基于ARX结构的新型序列密码算法FlashLight
算法·网络安全·密码学·哈希算法·同态加密
洛水水27 分钟前
【力扣100题】77.搜索二维矩阵
算法·leetcode·矩阵
仙俊红37 分钟前
深入理解 ThreadLocal —— 从变量引用、强弱引用到 Spring Boot 实战
spring boot·python·算法
故渊at41 分钟前
第五板块:Android 系统服务与电源管理 | 第十八篇:Battery Service 与 电量统计(Fuel Gauge)算法
android·算法·battery·电源·电池·电源管理·电量统计