当前位置:网站首页>Leetcode组合总和+剪枝
Leetcode组合总和+剪枝
2022-07-25 09:16:00 【Nuy.oah】
Leetcode39.组合总和
给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。

如图,首先有四种选择方法,可以在2,3,6.7中任选一个,然后将它加到临时列表中去,之后每层都有四种选择方法(因为可重复选择),每次选择后我们用sum+=选择的值,当我们一直选择到sum等于target的时候,说明我们找到了一种情况满足条件,那我们就将这一临时列表加到结果列表里去,如果sum>target,则说明当前的值不应该选择,直接return即可。只有当sumtarget时,才可以进行下去,遍历整数数组,然后加入当前数值,回溯,撤销。
public static void main(String[] args) {
int[] candidates={
2,3,6,7};
int target=7;
List<List<Integer>> list = combinationSum(candidates, target);
System.out.println(list);
}
public static List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> list=new ArrayList<>();
List<Integer> path=new ArrayList<>();
Arrays.sort(candidates);//必须先排序,防止重复
dfs(list,candidates,0,0,target,path);
return list;
}
private static void dfs(List<List<Integer>> list, int[] nums, int start, int sum, int target, List<Integer> path) {
if(sum==target){
list.add(new ArrayList<>(path));
return;
}
if (sum>target)//可有可无
return;
for (int i = start; (i < nums.length&&sum+nums[i]<=target); i++) {
//如果sum大于target就没有必要循环了
path.add(nums[i]);
dfs(list,nums,i,sum+nums[i],target,path);
path.remove(path.size()-1);
}
}
边栏推荐
- Ctfhub skill tree Web
- How to write the code of wechat applet implementation tab
- The development of art NFT
- activemq--可持久化机制之JDBC的journal
- [BUUCTF-n1book][第二章 web进阶]SSRF Training
- The annualization of financial products is 4%. How much profit can you get from buying 10000 yuan a month?
- A picture to quickly understand envoyfilter in istio
- leetcode-238.除自身以外数组的乘积
- [stl]stack & queue simulation implementation
- Django4.0 + web + MySQL 5.7 realize simple login operation
猜你喜欢

Live broadcast preview | how to build an enterprise cloud management platform in the cloudy era?
[learn rust together] a preliminary understanding of rust package management tool cargo

(self drawn ugly picture) simple understanding tcp/ip three handshakes and four waves

How to connect tdengine with idea database tool?
![[SCADA case] myscada helps VIB company realize the modernization and upgrading of production line](/img/67/b8c397d78a675014b5e08ceefc88dc.png)
[SCADA case] myscada helps VIB company realize the modernization and upgrading of production line

Canvas text JS special effect composed of many circles

Comments on specific applications of camera

Canvas dynamic picture avatar shaking JS special effect

js触屏小游戏源码冰雪之旅

Neural network learning (1) Introduction
随机推荐
activemq--可持久化机制之AMQ
c语言中的六个存储类型:auto register static extern const volatile
富文本样式文字图片处理
Software examination system architecture designer concise tutorial | software life cycle
图解LeetCode——1184. 公交站间的距离(难度:简单)
Druid 查询超时配置的探究 → DataSource 和 JdbcTemplate 的 queryTimeout 到底谁生效?
[machine learning] Finally, the important steps of machine learning modeling have been clarified
360度拖拽全景图插件tpanorama.js
Comparison between symmetric encryption and asymmetric encryption
Full solution of JDBC API
[STL]list模拟实现
activemq--可持久化机制之KahaDB
Overview of redis/mysql knowledge
Sort out Huawei ap-3010dn_ V2 configuration create WiFi
Wechat applet obtains the data of ---- onenet and controls the on-board LED of STM32
Illustration leetcode - 919. Complete binary tree inserter (difficulty: medium)
Visual query (sp_helptext) -- quick query of stored procedures containing specified strings (with source code)
Sticky.js page scrolling div fixed position plug-in
OpenCV实现简单的人脸追踪
LabVIEW experiment - temperature detection system (experimental learning version)