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;
    }
}
相关推荐
倒头就睡的小比特21 小时前
算法竞赛C++常用的STL
c++·算法
小羊没烦恼!21 小时前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
猎头南楼1 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
旖旎夜光1 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wzdark1 天前
大规模并行计算中的负载均衡算法研究4
算法
Because_of_Her11 天前
并查集-听课笔记
笔记·算法·并查集
码流子1 天前
高速公路安全监测实践:碰撞监测预警+物联网底座,从感知到处置的闭环
大数据·人工智能·物联网·算法·架构
another heaven1 天前
【算法/C++ MD5算法能否逆解码?原理、C++实现与同类哈希算法对比】
c++·算法·哈希算法
wzdark1 天前
从算法设计模式看编程思维的抽象能力4
算法
2601_962218611 天前
万象生鲜系统称重自动多退少补算法解决生鲜非标品痛点
大数据·数据库·人工智能·python·算法