首页 > 编程语言 >代码随想录算法训练营,9月19日 | 39. 组合总和,40.组合总和II,131.分割回文串

代码随想录算法训练营,9月19日 | 39. 组合总和,40.组合总和II,131.分割回文串

时间:2024-09-19 15:02:30浏览次数:1  
标签:return target 组合 int 随想录 candidates new path 总和

39. 组合总和
题目链接:39. 组合总和
文档讲解︰代码随想录(programmercarl.com)
视频讲解︰组合总和
日期:2024-09-19

想法:组合总和类型题,允许重复使用元素,递归不+1就行。
Java代码如下:

class Solution {
    List<Integer> path = new ArrayList<>();
    List<List<Integer>> res = new ArrayList<>();
    public void backTracking(int[] candidates, int target, int startIndex){
        if(target < 0){
            return;
        }
        if(target == 0){
            res.add(new ArrayList(path));
            return;
        }
        for(int i = startIndex; i < candidates.length; i++){
            path.add(candidates[i]);
            target -= candidates[i];
            backTracking(candidates, target, i);
            path.removeLast();
            target += candidates[i];
        }
    }
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        backTracking(candidates, target, 0);
        return res;
    }
}

40.组合总和II
题目链接:40.组合总和II
文档讲解︰代码随想录(programmercarl.com)
视频讲解︰组合总和II
日期:2024-09-19

想法:去重挺麻烦,第一遍没思路。
Java代码如下:

class Solution {
    List<Integer> path = new ArrayList<>();
    List<List<Integer>> res = new ArrayList<>();
    boolean[] used;
    public void backTracking(int[] candidates, int target, int startIndex){
        if(target < 0) return;
        if(target == 0){
            res.add(new ArrayList(path));
            return;
        }
        for(int i = startIndex; i < candidates.length; i++){
            if(i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false){
                continue;
            }
            path.add(candidates[i]);
            used[i] = true;
            target -= candidates[i];
            backTracking(candidates, target, i + 1);
            path.removeLast();
            target += candidates[i];
            used[i] = false;
        }
    }
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        used = new boolean[candidates.length];
        Arrays.fill(used, false);
        Arrays.sort(candidates);
        backTracking(candidates, target, 0);
        return res;
    }
}

总结:使用boolean used数组来表示数组中元素用没用,先将数组排序,使同样的数字在一起,uesd数组巧妙就巧妙在,如果used[i-1]为true,说明现在的i是在树枝上,used[i-1]为false,说明i现在是在同一树层上,这时判断candidates[i] == candidates[i - 1]就能直到是不是在同一层用过前一个相同大小的数了,做到了去重。在理解了用used数组的基础上就可以考虑不用它:

class Solution {
    List<Integer> path = new ArrayList<>();
    List<List<Integer>> res = new ArrayList<>();
    public void backTracking(int[] candidates, int target, int startIndex){
        if(target < 0) return;
        if(target == 0){
            res.add(new ArrayList(path));
            return;
        }
        for(int i = startIndex; i < candidates.length; i++){
            if(i > startIndex && candidates[i] == candidates[i - 1]){
                continue;
            }
            path.add(candidates[i]);
            target -= candidates[i];
            backTracking(candidates, target, i + 1);
            path.removeLast();
            target += candidates[i];
        }
    }
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        backTracking(candidates, target, 0);
        return res;
    }
}

在(横向)同一层的时候需要判断前一个数是否和现在的相等,而递归(纵向)时就正常i+1往下走就行。

131.分割回文串
题目链接:131.分割回文串
文档讲解︰代码随想录(programmercarl.com)
视频讲解︰分割回文串
日期:2024-09-19

想法:构成树的思路,横向,切一刀,从不同位置从左到右,纵向每一层在之前切一刀的位置的后面加1刀。
Java代码如下:

class Solution {
    List<List<String>> res = new ArrayList<>();
    List<String> path = new ArrayList<>();

    private 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();
            }
        }
    }

    private boolean check(StringBuilder sb){
        for(int i = 0, j = sb.length() - 1; i < j; i++, j--){
            if(sb.charAt(i) != sb.charAt(j)) return false;
        }
        return true;
    }

    public List<List<String>> partition(String s){
        backtracking(s, 0, new StringBuilder());
        return res;
    }

}

总结:判断回文双指针。

标签:return,target,组合,int,随想录,candidates,new,path,总和
From: https://www.cnblogs.com/wowoioo/p/18420585

相关文章

  • 代码随想录总结篇
    数组一般是排序以及索引问题链表翻转,重组,环问题哈希(map)hash暂存结果实现时间复杂度缩小字符串翻转,kmp算法,最长公共序列双指针快慢指针,左右指针栈队列先入先出后入先出二叉树二叉树dfs,bfsdfs计算深度高度二叉搜索树相关问题回溯递归三部曲参数返回值,终止条......
  • 分公司=一部门——组合模式
    文章目录分公司=一部门——组合模式分公司不就是一部门吗?组合模式透明方式与安全方式何时使用组合模式公司管理系统组合模式好处分公司=一部门——组合模式分公司不就是一部门吗?时间:5月10日19点地点:小菜、大鸟住所的客厅人物:小菜、大鸟“大鸟,请教你一个问......
  • Day 19 回溯法part01| LeetCode 77.组合,216. 组合总和 III,17. 电话号码的字母组合
    理论基础回溯法(回溯搜索法)回溯函数就是递归函数本质是穷举解决的问题组合问题(不强调元素顺序,需去重)切割问题子集问题:一个N个数的集合里有多少符合条件的子集排列问题(强调元素顺序)棋盘问题:N皇后回溯法模板(可抽象为树形结构——N叉树来解决问题)递归返回值以及......
  • 正式一刷代码随想录-day01
    数组T35 搜索插入位置1.想清楚边界,是否需要left<=right2.想清楚如果没有找到的几种情况,有没有遗漏的情况。3.此题需要注意返回的不可超过边界值。T34  在排序数组中查找元素的第一个和最后一个位置1.分析题目:三种情况:1.target不在数组大小的范围内2.在范围内但不在数......
  • 代码随想录算法 - 回溯算法1
    题目177.组合给定两个整数n和k,返回范围[1,n]中所有可能的k个数的组合。你可以按任何顺序返回答案。示例1:输入:n=4,k=2输出:[[2,4],[3,4],[2,3],[1,2],[1,3],[1,4],]示例2:输入:n=1,k=1输出:[[1]]提示:1<=n<=201<=k<=......
  • 代码随想录算法训练营,9月18日 | 77.组合,216.组合总和III,17.电话号码的字母组合
    回溯算法理论基础:1.回溯是递归的副产品,有递归就有回溯。2.回溯的本质是穷举,想让回溯法高效些,可以加一些剪枝的操作3.组合问题:N个数里面按一定规则找出k个数的集合切割问题:一个字符串按一定规则有几种切割方式子集问题:一个N个数的集合里有多少符合条件的子集排列问题:N个数按......
  • 【逐行解析】PSINS工具箱中的UKF组合导航的代码解析(test_SINS_GPS_UKF_153)
    详解工具箱的UKF153代码,最后给出运行结果的解读和代码修改思路文章目录工具箱程序简述函数详解运行结果解读修改思路修改后的结果工具箱本程序需要在安装工具箱后使用,工具箱是开源的,链接:http://www.psins.org.cn/kydm程序简述程序实现基于UKF(无迹卡尔曼滤波)的SI......
  • 【数据结构和算法实践-树-LeetCode112-路径总和】
    数据结构和算法实践-树-LeetCode112-路径总和题目MyThought代码示例JAVA-8题目给你二叉树的根节点root和一个表示目标和的整数targetSum。判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和targetSum。如果存在,返回true;否则......
  • 代码随想录Day3 | LeetCode 203. 移除链表元素、LeetCode 707. 设计链表、LeetCode 20
    LeetCode203.移除链表元素链表基础概念题,也可以用递归做,不过我们把递归的思想放在更能体现它的LeetCode206.反转链表#Definitionforsingly-linkedlist.#classListNode:#def__init__(self,val=0,next=None):#self.val=val#self.next......
  • 代码随想录Day4 | LeetCode 24. 两两交换链表中的节点、LeetCode 19. 删除链表的倒数
    LeetCode24.两两交换链表中的节点递归思想#Definitionforsingly-linkedlist.#classListNode:#def__init__(self,val=0,next=None):#self.val=val#self.next=nextclassSolution:defswapPairs(self,head:Optional[ListNode......