当前位置:网站首页>Leetcode t39: combined sum
Leetcode t39: combined sum
2022-07-01 08:18:00 【Fan Qianzhi】
Title Description
To give you one No repeating elements Array of integers for candidates And a target integer target , find candidates You can make the sum of numbers as the target number target Of all Different combinations , And return... As a list . You can press In any order Return these combinations .
candidates Medium The same Numbers can Unlimited repeat selected . If the selected number of at least one number is different , Then the two combinations are different .
For a given input , Guarantee and for target The number of different combinations of is less than 150 individual .
Example 1:
Input :candidates = [2,3,6,7], target = 7
Output :[[2,2,3],[7]]
explain :
2 and 3 Can form a set of candidates ,2 + 2 + 3 = 7 . Be careful 2 You can use it multiple times .
7 Is also a candidate , 7 = 7 .
Only these two combinations .
Example 2:
Input : candidates = [2,3,5], target = 8
Output : [[2,2,2,2],[2,3,3],[3,5]]
Example 3:
Input : candidates = [2], target = 1
Output : []
Ideas
Very conventional dfs topic , But I haven't played for a long time dfs Code. , Make a note of .
Code
List<List<Integer>> res = new ArrayList<List<Integer>>();
int n, tar;
int[] nums;
// Currently selected combination , Select the number , The present and
void dfs(List<Integer> lis, int cur, int s) {
if(cur == n) {
if(s == tar)
res.add(lis);
} else {
// for(int x: lis) System.out.print(x+"\t"); System.out.println("\t,"+cur+"-"+s);
// Optional for the current number , Optional
// choose , See if it exceeds , prune
if(s + nums[cur] <= tar) {
List<Integer> tmp = new ArrayList<Integer>(lis);
tmp.add(nums[cur]);
dfs(tmp, cur, s + nums[cur]);
}
// No election
if(true) {
List<Integer> tmp = new ArrayList<Integer>(lis);
dfs(tmp, cur+1, s);
}
}
}
public List<List<Integer>> combinationSum(int[] candidates, int target) {
nums = candidates;
n = candidates.length;
tar = target;
dfs(new ArrayList<Integer>(), 0, 0);
return res;
}
边栏推荐
- Yolov5进阶之七目标追踪最新环境搭建
- Microsoft stream - how to modify video subtitles
- window c盘满了
- Embedded-c language-10-enumeration / (function) pointer (function) / multi-level pointer /malloc dynamic allocation / file operation
- Leetcode T39: 组合总和
- Learn reptiles for a month and earn 6000 a month? Tell you the truth about the reptile, netizen: I wish I had known it earlier
- Aardio - [problem] the problem of memory growth during the callback of bass Library
- PostgreSQL source code learning (26) -- windows vscode remote debugging PostgreSQL on Linux
- XX attack - reflective XSS attack hijacking user browser
- Yolov5进阶之六目标追踪环境搭建
猜你喜欢
![[batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)](/img/48/de19e8cc007b93a027a906d4d423b2.png)
[batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)

OJ input and output exercise

Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field

Airsim radar camera fusion to generate color point cloud

web254

Office365 - how to use stream app to watch offline files at any time

CPU设计实战-第四章实践任务一简单CPU参考设计调试

Set up file server Minio for quick use

【入门】输入整型数组和排序标识,对其元素按照升序或降序进行排序

Access报表实现小计功能
随机推荐
Leetcode T39: 组合总和
图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
Instead of houses, another kind of capital in China is rising
Yolov5进阶之六目标追踪环境搭建
【Redis】一气呵成,带你了解Redis安装与连接
seaborn clustermap矩阵添加颜色块
Using settoolkit to forge sites to steal user information
OJ input and output exercise
shardingSphere
事务方法调用@Transactional
web254
Data analysis notes 11
Implementation and encapsulation of go universal dynamic retry mechanism
Airsim雷达相机融合生成彩色点云
Lm08 mesh series mesh inversion (fine)
[untitled]
Analysis of slice capacity expansion mechanism
Luogu p1088 [noip2004 popularization group] Martians
Teach you how to apply for domestic trademark online step by step
程序员养生宝典