首页 > 其他分享 >力扣回溯之 39. 组合总和

力扣回溯之 39. 组合总和

时间:2024-03-08 11:48:59浏览次数:19  
标签:39 target int res sum 力扣 candidates 回溯 path

// 剪枝优化 class Solution {     public List<List<Integer>> combinationSum(int[] candidates, int target) {         List<List<Integer>> res = new ArrayList<>();         List<Integer> path = new ArrayList<>();         Arrays.sort(candidates); // 先进行排序         backtracking(res, path, candidates, target, 0, 0);         return res;     }
    public void backtracking(List<List<Integer>> res, List<Integer> path, int[] candidates, int target, int sum,             int idx) {         // 找到了数字和为 target 的组合         if (sum == target) {             res.add(new ArrayList<>(path));             return;         }
        for (int i = idx; i < candidates.length; i++) {             // 如果 sum + candidates[i] > target 就终止遍历             if (sum + candidates[i] > target) {                 break;             }             path.add(candidates[i]);             backtracking(res, path, candidates, target, sum + candidates[i], i);             path.remove(path.size() - 1); // 回溯,移除路径 path 最后一个元素         }     } }

标签:39,target,int,res,sum,力扣,candidates,回溯,path
From: https://www.cnblogs.com/JavaYuYin/p/18060631

相关文章

  • 【力扣】组合总和3(组合的去重)
    问题描述注意,如果数组里有两个元素的值相同,那么这两个元素是可以出现在同一个组合里的:但是:如果按前面的思路分析的话,会发现结果中出现很多相同的组合。像这样:这很明显是由于两个相同的1造成的,因为当前的startindex对应第一个1时,向下一层递归后,starindex定位的还是1,。如......
  • Vue学习笔记39--创建Vue脚手架
    创建Vue脚手架1.Vue脚手架是Vue官方提供的标准开发工具(开发平台)2.脚手架最新版本4.x3.文档:https://cli.vuejs.org/zh/操作步骤:第一步:(仅第一次执行):全局安装@vue/cli(commandlineinterface)注:安装钱建议先设置镜像==》npmconfigsetregisterhttps://registry.npm.taoba......
  • 【力扣】组合总数(回溯法)
    题目描述#include<iostream>#include<vector>usingnamespacestd;vector<vector<int>>res;vector<int>path;intaccumulate(vector<int>path){ intsum; for(inti=0;i<path.size();i++){ sum+=path[i]; } r......
  • CF38E Let's Go Rolling! 题解
    分析考虑DP。因为\(n\le3000\),我们可以直接枚举插针的位置。定义状态函数\(f_i\)表示在从左往右第\(i\)个小球的位置上插针的最小花费。枚举该小球右边第一个插针的位置,则\(i\)到\(j-1\)的小球都会滚到小球\(i\)的位置。代价为\(\sum\limits_{k=i}^{j-1}x_k-x_i......
  • P6390 [COCI2007-2008#4] POKLON 题解
    感谢@\(\color{#AEF}{\texttt{CelestialCyan}}\)大神对我的骚扰帮助。分析一眼DP。对于求最大满足条件区间数,我们定义状态函数\(\mathit{f}_{i}\)表示在第\(1\)到\(i\)个区间中选择,且必选第\(i\)个区间能够得到的最大长度。有转移方程:\(\mathit{f}_{i}=\max\{f[j]|......
  • 【力扣】电话号码的组合(回溯法)
    问题描述classSolution{public:vector<string>res;stringpath;//charA[26]={'a','b','c','d','e','f','g',//'h','i','j','k&......
  • 耳分解、双极定向和 P9394 Solution
    耳分解设无向图\(G'(V',E')\subsetG(V,E)\),简单路径或简单环\(P:x_1\to\dots\tox_k\)被称为\(G\)关于\(G'\)的耳,当且仅当其满足\(x_1,x_k\inV',x_2,x_3\dotsx_{k-1}\not\inV'\)。如果\(P\)是简单路径,那么\(P\)称为开耳。下面记树上\(x,y\)之间的路......
  • 【力扣】求组合(回溯算法)
    题目描述2.以下是回溯算法的模版classSolution{private:vector<vector<int>>res;vector<int>path;//这个变量名还是设为path更合适voidbacktrace(intn,intk,intstartindex){//递归结束条件,这个是根据问题要求修改的if(path.s......
  • The Marvels of an Electronic Technician's World
    Welcometothefascinatingworldofelectronictechnicians!Inthisblogpost,wewilldiveintotheexcitingrealmoftheseunsungheroeswhoworkbehindthescenestokeepourgadgetsrunningsmoothly.Fromtroubleshootingtorepairing,anelectronicte......
  • Abbott的复仇 Abbott's Revenge
    原题链接bfs的深度用法。这题最坑的我觉得是输入输出格式的处理(一不小心就容易格式错误)调了好几个小时.....这里放一组udebug数据SAMPLE31N3311WLNR*12WLNRERWF*13NLER*21SLWRNF*22SLWFELF*23SFREL*0NOSOLUTION31N3211WLNR......