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:

  1. Input: candidates = [10,1,2,7,6,1,5], target = 8,
  2. A solution set is:
  3. [
  4. [1, 7],
  5. [1, 2, 5],
  6. [2, 6],
  7. [1, 1, 6]
  8. ]

Example 2:

  1. Input: candidates = [2,5,2,1,2], target = 5,
  2. A solution set is:
  3. [
  4.   [1,2,2],
  5.   [5]
  6. ]

这道题跟之前那道 Combination Sum 本质没有区别,只需要改动一点点即可,之前那道题给定数组中的数字可以重复使用,而这道题不能重复使用,只需要在之前的基础上修改两个地方即可,首先在递归的 for 循环里加上 if (i > start && num[i] == num[i - 1]) continue; 这样可以防止 res 中出现重复项,然后就在递归调用 helper 里面的参数换成 i+1,这样就不会重复使用数组中的数字了,代码如下:

  1. class Solution {
  2. public:
  3. vector<vector<int>> combinationSum2(vector<int>& num, int target) {
  4. vector<vector<int>> res;
  5. vector<int> out;
  6. sort(num.begin(), num.end());
  7. helper(num, target, , out, res);
  8. return res;
  9. }
  10. void helper(vector<int>& num, int target, int start, vector<int>& out, vector<vector<int>>& res) {
  11. if (target < ) return;
  12. if (target == ) { res.push_back(out); return; }
  13. for (int i = start; i < num.size(); ++i) {
  14. if (i > start && num[i] == num[i - ]) continue;
  15. out.push_back(num[i]);
  16. helper(num, target - num[i], i + , out, res);
  17. out.pop_back();
  18. }
  19. }
  20. };

Github 同步地址:

https://github.com/grandyang/leetcode/issues/40

类似题目:

Combination Sum III

Combination Sum

参考资料:

https://leetcode.com/problems/combination-sum-ii/

https://leetcode.com/problems/combination-sum-ii/discuss/16861/Java-solution-using-dfs-easy-understand

https://leetcode.com/problems/combination-sum-ii/discuss/16878/Combination-Sum-I-II-and-III-Java-solution-(see-the-similarities-yourself)

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 40. Combination Sum II 组合之和之二的更多相关文章

  1. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  2. [LeetCode] 40. Combination Sum II 组合之和 II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  3. [LeetCode] Combination Sum II 组合之和之二

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  4. [LeetCode] 377. Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  5. [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 ...

  6. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  7. [LeetCode] 377. Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  8. LeetCode OJ:Combination Sum II (组合之和 II)

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  9. LeetCode 40. Combination Sum II (组合的和之二)

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

随机推荐

  1. STS 重写父类方法的操作

    本来这种东西真的没什么好写的,但是很多时候真的是要用到的,不知道的话自己手动敲,会累死人的.所以记录在这里,自己的笔记,有需要的赶紧拿去,省的手动录入那么辛苦. 在代码窗口点击右键,依次选择“Sour ...

  2. 七、Spring之深入理解AOP源码

    Spring之深入理解AOP源码 ​ 在上一篇博文中,我们对AOP有了初步的了解,那么接下来我们就对AOP的实现原理进行深入的分析. ​ 在之前写的那个AOP示例代码当中有这样一个注解:@Enable ...

  3. .NET Core 多框架支持(net45+netstandard20)实践中遇到的一些问题总结

    .NET Core 多框架支持(net45+netstandard20)实践中遇到的一些问题总结 前言 本文主要是关于.NET Standard 代码 在多框架 和 多平台 支持自己实践过程中遇到的一 ...

  4. Enum.GetUnderlyingType(obj.GetType())

    Enum.GetUnderlyingType(obj.GetType())获取保存枚举值的数据类型:

  5. QT+OpenGL(02)-- zlib库的编译

    1.zlib库的下载 http://www.zlib.net/ zlib1211.zip 2.解压 3.进入  zlib1211\zlib-1.2.11\contrib\vstudio\vc14 目录 ...

  6. WinForms中动态给treeView的节点添加ContextMenuStrip,并绑定Click事件

    生成ContextMenuStrip var docMenu = new ContextMenuStrip(); ToolStripMenuItem deleteMenuItem = new Tool ...

  7. 微信小程序navigator页面跳转失效原因

    在编写小程序时遇到一个问题:使用 <navigator url='/pages/lists/index'>...</navigator>进行跳转没有反应.控制台也没有报错,ap ...

  8. Java并发包——线程安全的Map相关类

    Java并发包——线程安全的Map相关类 摘要:本文主要学习了Java并发包下线程安全的Map相关的类. 部分内容来自以下博客: https://blog.csdn.net/bill_xiang_/a ...

  9. Laravel 运行php artisan serve命令时提示No application encryption key has been specified

    创建了新的laravel项目后, 运行提示:No application encryption key has been specified 解决方法: 这个是由于没有配置好 APP_KEY 在终端上 ...

  10. redis笔记3

    redis持久化机制 redis提供了两种持久化策略 RDB RDB的持久化策略: 按照规则定时将内存的数据同步到磁盘 snapshot redis在指定的情况下会触发快照 自己配置的快照规则 sav ...