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

类似题目:

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. python生成多维数组方法总结(多维创建有问题的情况)

    1.一维 list1=[]* #[,,,,] list2=np.arange() #[,,,,] 2.二维(注意) list2=[[]*]* 这种创建是有问题的!! print(list2)list2 ...

  2. idea配置热加载

    第一步:添加依赖 spring-boot项目中引入如下依赖 <dependency> <groupId>org.springframework.boot</groupId ...

  3. vue中引入mintui、vux重构简单的APP项目

    最近在学习vue时也了解到一些常用的UI组件,有用于PC的和用于移动端的.用于PC的有:Element(饿了么).iView等:用于移动端APP的有Vux.Mint UI(饿了么).Vant(有赞团队 ...

  4. 基于Task定时检测网络本地网络状况

    首先我们需要使用winInet.dll中的InternetGetConnectedState方法来检测本地是否连接网络,然后再通过ping的方式来获取网络状况. 然后我们采用Task来开辟一个线程来定 ...

  5. webservice因引用Oracle.DataAccess.dll导致发布前预编译不通过

    这个问题最初是什么问题已经忘了,虽然就在几小时前/

  6. WInforn中设置ZedGraph的焦点显示坐标格式化以及显示三个坐标数的解决办法

    场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...

  7. Codeforces 939A题,B题(水题)

    题目链接:http://codeforces.com/problemset/problem/939/A A题 A. Love Triangle time limit per test 1 second ...

  8. Qt程序app添加图标复制到其它电脑后不显示的解决方法

    原因: 主是要因为Qt显示图标需要依赖一些库来进行转换,而复制到其它电脑后不显示,是没有复制相应的库所致,所以把相应库复制过去就行了. 复制Qt的plugins目录下的imageformats文件到程 ...

  9. pipenv管理python开发环境

    简介 简单说,pipenv就是把pip和virtualenv包装起来的一个便携工具. 它不会在你的项目文件夹里生成一大堆东西,只有两个文本文件: Pipfile, 简明地显示项目环境和依赖包. Pip ...

  10. MariaDB设置主从复制

    主从复制包含两个步骤: 在 master 主服务器(组)上的设置,以及在 slave 从属服务器(组)上的设置. 配置主服务器 master 如果没有启用,则需要 激活二进制日志. 给 master ...