[LeetCode] 40. Combination Sum II 组合之和之二
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]
- ]
这道题跟之前那道 Combination Sum 本质没有区别,只需要改动一点点即可,之前那道题给定数组中的数字可以重复使用,而这道题不能重复使用,只需要在之前的基础上修改两个地方即可,首先在递归的 for 循环里加上 if (i > start && num[i] == num[i - 1]) continue; 这样可以防止 res 中出现重复项,然后就在递归调用 helper 里面的参数换成 i+1,这样就不会重复使用数组中的数字了,代码如下:
- class Solution {
- public:
- vector<vector<int>> combinationSum2(vector<int>& num, int target) {
- vector<vector<int>> res;
- vector<int> out;
- sort(num.begin(), num.end());
- helper(num, target, , out, res);
- return res;
- }
- void helper(vector<int>& num, int target, int start, vector<int>& out, vector<vector<int>>& res) {
- if (target < ) return;
- if (target == ) { res.push_back(out); return; }
- for (int i = start; i < num.size(); ++i) {
- if (i > start && num[i] == num[i - ]) continue;
- out.push_back(num[i]);
- helper(num, target - num[i], i + , out, res);
- out.pop_back();
- }
- }
- };
Github 同步地址:
https://github.com/grandyang/leetcode/issues/40
类似题目:
参考资料:
https://leetcode.com/problems/combination-sum-ii/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 40. Combination Sum II 组合之和之二的更多相关文章
- [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] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [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 ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [LeetCode] 377. Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- LeetCode OJ:Combination Sum II (组合之和 II)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode 40. Combination Sum II (组合的和之二)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
随机推荐
- STS 重写父类方法的操作
本来这种东西真的没什么好写的,但是很多时候真的是要用到的,不知道的话自己手动敲,会累死人的.所以记录在这里,自己的笔记,有需要的赶紧拿去,省的手动录入那么辛苦. 在代码窗口点击右键,依次选择“Sour ...
- 七、Spring之深入理解AOP源码
Spring之深入理解AOP源码 在上一篇博文中,我们对AOP有了初步的了解,那么接下来我们就对AOP的实现原理进行深入的分析. 在之前写的那个AOP示例代码当中有这样一个注解:@Enable ...
- .NET Core 多框架支持(net45+netstandard20)实践中遇到的一些问题总结
.NET Core 多框架支持(net45+netstandard20)实践中遇到的一些问题总结 前言 本文主要是关于.NET Standard 代码 在多框架 和 多平台 支持自己实践过程中遇到的一 ...
- Enum.GetUnderlyingType(obj.GetType())
Enum.GetUnderlyingType(obj.GetType())获取保存枚举值的数据类型:
- QT+OpenGL(02)-- zlib库的编译
1.zlib库的下载 http://www.zlib.net/ zlib1211.zip 2.解压 3.进入 zlib1211\zlib-1.2.11\contrib\vstudio\vc14 目录 ...
- WinForms中动态给treeView的节点添加ContextMenuStrip,并绑定Click事件
生成ContextMenuStrip var docMenu = new ContextMenuStrip(); ToolStripMenuItem deleteMenuItem = new Tool ...
- 微信小程序navigator页面跳转失效原因
在编写小程序时遇到一个问题:使用 <navigator url='/pages/lists/index'>...</navigator>进行跳转没有反应.控制台也没有报错,ap ...
- Java并发包——线程安全的Map相关类
Java并发包——线程安全的Map相关类 摘要:本文主要学习了Java并发包下线程安全的Map相关的类. 部分内容来自以下博客: https://blog.csdn.net/bill_xiang_/a ...
- Laravel 运行php artisan serve命令时提示No application encryption key has been specified
创建了新的laravel项目后, 运行提示:No application encryption key has been specified 解决方法: 这个是由于没有配置好 APP_KEY 在终端上 ...
- redis笔记3
redis持久化机制 redis提供了两种持久化策略 RDB RDB的持久化策略: 按照规则定时将内存的数据同步到磁盘 snapshot redis在指定的情况下会触发快照 自己配置的快照规则 sav ...