当前位置:网站首页>Leetcode T40: 组合总和II
Leetcode T40: 组合总和II
2022-07-01 08:08:00 【范谦之】
题目描述
给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用 一次 。
注意:解集不能包含重复的组合。
示例 1:
输入: candidates = [10,1,2,7,6,1,5], target = 8,
输出:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]
示例 2:
输入: candidates = [2,5,2,1,2], target = 5,
输出:
[
[1,2,2],
[5]
]
提示
1 <= candidates.length <= 100
1 <= candidates[i] <= 50
1 <= target <= 30
思路
和上一题:组合总和I一样,都是使用dfs,但是,如果仍采用上一题的思路,尽管进行了剪枝,仍会超时。这里采用另一种dfs思路。
由于每个candidate[i]的范围都在[0, 50],所以可以定义一个数组记录每个元素的个数,然后根据这个数组进行dfs搜索。
代码
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];
// 当前选取的组合,选到了第几个数字,当前和, 剩下和
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 {
//对于当前数字
//选,看是否超过,剪枝
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);
}
}
// 不选,如果剩下的都装仍不能满足,则剪枝
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;
}
边栏推荐
- golang中的正则表达式使用注意事项与技巧
- 【批处理DOS-CMD-汇总】扩展变量-延迟变量cmd /v:on、cmd /v:off、setlocal enabledelayedexpansion、DisableDelayedExpansion
- Chinese font Gan: zi2zi
- 【入门】提取不重复的整数
- Array: question brushing record
- 【入门】输入n个整数,输出其中最小的k个
- 【刷题】字符统计【0】
- [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)
- 图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
- Gdip - hatchBrush图案表
猜你喜欢

postgresql源码学习(26)—— Windows vscode远程调试Linux上的postgresql

Cyclic neural network

【网站架构】一招搞定90%的分布式事务,实打实介绍数据库事务、分布式事务的工作原理应用场景

谈谈数字化转型的几个关键问题

Differential: definition of total differential, partial derivative, gradient

P4 installation bmv2 detailed tutorial
![[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 (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)

【无标题】

01 NumPy介绍

Utiliser Beef pour détourner le navigateur utilisateur
随机推荐
手工挖XSS漏洞
[getting started] extract non repeating integers
Embedded-c language-10-enumeration / (function) pointer (function) / multi-level pointer /malloc dynamic allocation / file operation
QT -- 1. QT connection database
使用threejs简单Web3D效果
Latex table
[batch DOS CMD summary] extension variables - delay variables CMD /v:on, CMD /v:off, SETLOCAL enabledelayedexpansion, disabledelayedexpansion
web254
SharePoint - modify web application authentication using PowerShell
When using charts to display data, the time field in the database is repeated. How to display the value at this time?
Principle and process of embossing
Two expressions of string
Aardio - Method of self constructed geticonhandle
php laravel微信支付
EDA open source simulation tool verilator beginner 6: debugging examples
postgresql源码学习(26)—— Windows vscode远程调试Linux上的postgresql
How to check ad user information?
【力扣10天SQL入门】Day10 控制流
Access报表实现小计功能
使用beef劫持用户浏览器