当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

Access报表实现小计功能

PostgreSQL source code learning (26) -- windows vscode remote debugging PostgreSQL on Linux
![[staff] high and low octave mark (the notes in the high octave mark | mark range are increased by one octave as a whole | low octave mark | mark range are decreased by one octave as a whole)](/img/ff/ebd936eaa6e57b1eabb691b0642957.jpg)
[staff] high and low octave mark (the notes in the high octave mark | mark range are increased by one octave as a whole | low octave mark | mark range are decreased by one octave as a whole)

使用beef劫持用户浏览器

Principle and process of embossing
![[untitled]](/img/b9/6922875009c2d29224a26ed2a22b01.jpg)
[untitled]

Gdip - hatchBrush图案表

手工挖XSS漏洞

Utiliser Beef pour détourner le navigateur utilisateur

Manually dig XSS vulnerabilities
随机推荐
[staff] key number (key number identification position | key number marking list | a major key identification principle | F, C, G position marking ascending | F major key identification principle | B
Contenttype comparison of all types
谈谈数字化转型的几个关键问题
How outlook puts together messages with the same discussion
[batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)
Leetcode T40: 组合总和II
Cmake I two ways to compile source files
Transaction method call @transactional
Provincial selection + noi Part II string
On several key issues of digital transformation
Luogu p3799 demon dream stick
Gdip - hatchbrush pattern table
SharePoint - how to quickly check whether SharePoint is standard or enterprise edition?
Aardio - Method of self constructed geticonhandle
使用beef劫持用戶瀏覽器
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
【刷题】字符统计【0】
When using charts to display data, the time field in the database is repeated. How to display the value at this time?
Precautions and skills in using regular expressions in golang
Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system