当前位置:网站首页>Leetcode t40: combined sum II
Leetcode t40: combined sum II
2022-07-01 08:18:00 【Fan Qianzhi】
Title Description
Given a set of candidate numbers candidates And a target number target , find candidates All can make numbers and target The combination of .
candidates Each number in can only be used in each combination once .
Be careful : A solution set cannot contain duplicate combinations .
Example 1:
Input : candidates = [10,1,2,7,6,1,5], target = 8,
Output :
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]
Example 2:
Input : candidates = [2,5,2,1,2], target = 5,
Output :
[
[1,2,2],
[5]
]
Tips
1 <= candidates.length <= 100
1 <= candidates[i] <= 50
1 <= target <= 30
Ideas
And the last question : Combinatorial summation I equally , Is to use dfs, however , If you still use the idea of the previous question , Despite the pruning , Still timeout . Here is another dfs Ideas .
Because each candidate[i] It's all in the range of [0, 50], So you can define an array to record the number of each element , Then proceed according to this array dfs Search for .
Code
List<List<Integer>> res = new ArrayList<List<Integer>>();
HashMap<List<Integer>, Integer> map = new HashMap<List<Integer>, Integer>();
int tar;
int[] nums = new int[51];
// Currently selected combination , Select the number , The present and , The rest and
void dfs(List<Integer> lis, int cur, int s, int ts) {
if(cur == 51) {
if(s == tar && map.getOrDefault(lis, -1)==-1) {
map.put(lis, 1);
}
} else {
// for(int x: lis) System.out.print(x+","); System.out.println("\t"+cur+","+s);
if(nums[cur] == 0) dfs(lis, cur+1, s, ts);
else {
// For the current number
// choose , See if it exceeds , prune
List<Integer> tmp = new ArrayList<Integer>(lis);
for(int i = 1; i <= nums[cur]; i++) {
if(s + i*cur <= tar) {
tmp.add(cur);
dfs(tmp, cur+1, s + i*cur, ts-i*cur);
}
}
// No election , If the rest is still not enough , Then prune
if(s+ts-cur >= tar) {
List<Integer> tmp2 = new ArrayList<Integer>(lis);
dfs(tmp2, cur+1, s, ts-cur);
}
}
}
}
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
tar = target;
for(int x: candidates) nums[x]++;
int s = 0;
for(int x: candidates) s += x;
dfs(new ArrayList<Integer>(), 0, 0, s);
for(List<Integer> lis: map.keySet()) {
res.add(lis);
}
return res;
}
边栏推荐
- [getting started] intercepting strings
- Conception et mise en service du processeur - chapitre 4 tâches pratiques
- seaborn clustermap矩阵添加颜色块
- Aardio - [problem] the problem of memory growth during the callback of bass Library
- Yolov5进阶之六目标追踪环境搭建
- Contenttype comparison of all types
- 使用beef劫持用户浏览器
- Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure
- Why are some Wills made by husband and wife invalid
- Embedded-c language-10-enumeration / (function) pointer (function) / multi-level pointer /malloc dynamic allocation / file operation
猜你喜欢

On several key issues of digital transformation

【入门】取近似值
![[website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions](/img/2c/07d729d49b1d74553decac4588074e.png)
[website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions

Learn the knowledge you need to know about the communication protocol I2C bus

0 basic introduction to single chip microcomputer: how to use digital multimeter and precautions

01 numpy introduction
![[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 (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)

Latex table

Five combination boxing, solving six difficult problems on campus and escorting the construction of educational informatization

shardingSphere
随机推荐
【刷题】字符统计【0】
【入门】输入n个整数,输出其中最小的k个
Burpsuite -- brute force cracking of intruder
Access报表实现小计功能
7-26 word length (input and output in the loop)
Provincial election + noi part I dynamic planning DP
【Redis】一气呵成,带你了解Redis安装与连接
Contenttype comparison of all types
On several key issues of digital transformation
Analysis of slice capacity expansion mechanism
Li Kou daily question - Day 32 -1822 Symbol of array element product
Koltin35, headline Android interview algorithm
How to get a SharePoint online site created using the office365 group template
[getting started] extract non repeating integers
[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)
【入门】取近似值
一套十万级TPS的IM综合消息系统的架构实践与思考
Php laraver Wechat payment
Lm08 mesh series mesh inversion (fine)
uni 热更新