LeetCode 40. Combination Sum II 组合总和 II (C++/Java)
题目:
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.
Each number in candidates may only be used once in the combination.
Note:
- All numbers (including
target) will be positive integers. - The solution set must not contain duplicate combinations.
Example 1:
Input: candidates =[10,1,2,7,6,1,5], target =8,
A solution set is:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]
Example 2:
Input: candidates = [2,5,2,1,2], target = 5,
A solution set is:
[
[1,2,2],
[5]
]
分析:
本题是LeetCode 39. Combination Sum 组合总和 (C++/Java)的扩展,我们还是利用39题中的方法来做这道题。
只不过本题所给的数字有重复,且要求最后的结果中不能有重复的,例如例子1,有两个1,他们都可以和7组合成8,如果按照39题搜索的方法就会产生重复结果。
其中一个办法就是将搜索生成的结果加入到set中,这样集合类会自动帮我们去重。
此外我们还可以在每一轮搜索时,当发现有相同元素时,直接跳过本次搜素,这样就不会产生重复的结果了。
程序:
C++
class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> res;
vector<int> curr;
sort(candidates.begin(), candidates.end());
dfs(candidates, target, 0, res, curr);
return res;
}
void dfs(vector<int>& candidates, int target, int index, vector<vector<int>>& res, vector<int> curr){
if(target == 0){
res.push_back(curr);
return;
}
for(int i = index; i < candidates.size(); ++i){
if(candidates[i] > target)
return;
if(i > index && candidates[i] == candidates[i-1])
continue;
curr.push_back(candidates[i]);
dfs(candidates, target-candidates[i], i+1, res, curr);
curr.pop_back();
}
}
};
Java
class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> curr = new LinkedList<>();
Arrays.sort(candidates);
dfs(candidates, target, 0, res, curr);
return res;
}
private void dfs(int[] candidates, int target, int index, List<List<Integer>> res, LinkedList<Integer> curr){
if(target == 0){
res.add(new LinkedList<>(curr));
return;
}
for(int i = index; i < candidates.length; ++i){
if(candidates[i] > target)
return;
if(i > index && candidates[i] == candidates[i-1])
continue;
curr.addLast(candidates[i]);
dfs(candidates, target-candidates[i], i+1, res, curr);
curr.removeLast();
}
}
}
LeetCode 40. Combination Sum II 组合总和 II (C++/Java)的更多相关文章
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [LeetCode] 216. Combination Sum III 组合之和 III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 40. Combination Sum II 组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 377. Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- LeetCode 40. Combination Sum II (组合的和之二)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode】Combination Sum II(组合总和 II)
这道题是LeetCode里的第40道题. 题目要求: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. can ...
- LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)
题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description 给定数组,数组中的元素均为正数,target也是正数. ...
随机推荐
- 微信小程序报错:Expecting 'STRING', got INVALID
具体错误如下图: 这是因为在微信小程序的 app.json 文件中是不能包含有注释的,只需要把注释去掉就可以了.
- 力扣592(java)-分数加减运算(中等)
题目: 给定一个表示分数加减运算的字符串 expression ,你需要返回一个字符串形式的计算结果. 这个结果应该是不可约分的分数,即最简分数. 如果最终结果是一个整数,例如 2,你需要将它转换成分 ...
- 合阔智云核心生产系统切换到服务网格 ASM 的落地实践
简介: 合阔智云提供了从全渠道交易管理到订单履约再到门店供应链完整的餐饮零售连锁解决方案,整个方案采取微服务设计,并深度使用了 Kubernetes 作为生产调度平台. 作者:刘如鸿 背景 合阔智 ...
- 比心云平台基于阿里云容器服务 ACK 的弹性架构实践
简介:本文主要探讨比心云平台如何利用阿里云容器服务 ACK,来构建应用弹性架构,进一步优化计算成本. 作者:韩韬|比心技术 前言 应用容器化改造后,不可避免地会面临这样一个问题:Kubernetes ...
- e签宝:借助钉钉宜搭变革传统项目管理模式,交付效率显著提升
简介:通过钉钉宜搭,e签宝在半个月内搭建了项目交付管理平台,提升了项目管理的效率和质量,推进了团队核心业务的信息化建设.e签宝在有效梳理了各环节的工作进度.质量.成本.职权后,通过宜搭平台保障了内外 ...
- [FAQ] golang-migrate/migrate error: migration failed in line 0: (details: Error 1065: Query was empty)
当我们使用 migrate create 创建了迁移文件. 没有及时填写内容,此时运行 migrate 的后续命令比如 up.down 会抛出错误: error: migration failed i ...
- WPF 已知问题 InputEventArgs 的 Timestamp 属性是静态的导致事件之间相互影响
本文记录一个 WPF 已知的设计问题,当前此问题已经被大佬修复,这个设计问题刚好属于比较边缘的模块,我写了这么多年的代码还没有踩到这个坑一次,也没有听到有谁提到这个坑 远古时候,不知道大佬是故意还是失 ...
- selenium操作浏览器模块
selenium模块用途 selenuim原先多用于测试部门测试,由于它可以操作浏览器,有时候也用于爬虫领域 优点:操作浏览器访问网站 缺点:速度较慢 下载模块 # 下载模块 pip3 install ...
- HttpClient配置SSL绕过https证书以及双向认证
HttpClient简介 1.HTTP 协议是 Internet 上使用得最多.最重要的协议之一,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 java ...
- go-zero goctl命令图解