[LeetCode] 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] 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 组合之和之二
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 ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- LeetCode OJ:Combination Sum II (组合之和 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] 377. Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
随机推荐
- ASP.NET 截获服务器生成的将要发送到客户端的html的方法
有时候我们需要在将服务器端生成的html发送带客户端之前对这些html进行操作,比如生成静态html加之保存.改变生成的html中的某些内容等等,那么久可以通过如下的方案解决. 我总结了两种方式,个人 ...
- NSSortDescriptor 的使用
NSSortDescriptor 是什么 ? 你可以将它看做是对一个排序规则的描述者 因为我们可以使用它来对我们数组中的对象进行排序操作 假设现在有这样一个需求: 数组里面有十个Person对象 ...
- C#定时执行
代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; ...
- C#开发微信门户及应用(20)-微信企业号的菜单管理
前面几篇陆续介绍了很多微信企业号的相关操作,企业号和公众号一样都可以自定义菜单,因此他们也可以通过API进行菜单的创建.获取列表.删除的操作,因此本篇继续探讨这个主体,介绍企业号的菜单管理操作. 菜单 ...
- 从ASP.NET 升级到ASP.NET5(RC1) - 翻译
前言 ASP.NET 5 是一次令人惊叹的对于ASP.NET的创新革命. 他将构建目标瞄准了 .NET Core CLR, 同时ASP.NET又是对于云服务进行优化,并且是跨平台的框架.很多文章已经称 ...
- 【转】MVC、MVP与MVT
MVC是Model-View-Control的缩写,Model指的是数据层,View指的是UI层,Control指的是控制层,这三层之间彼此联系.View层的用户行为,触发Control层,Contr ...
- Date.parse
JavaScript: Date.parse(),一个参数,参数类型是 JavaScript 中的 Date 类型. 返回值 : 得到一个 Unix 时间戳,比如说,1470993235000,这种东 ...
- 物联网框架SuperIO 2.2.9和ServerSuperIO 2.1同时更新,更适用于类似西门子s7-200发送多次数据,才能读取数据的情况
一.解决方案 二.更新内容 1.修改IRunDevice接口,把void Send(io,bytes)改成int Send(io,bytes).2.修改网络控制器,发送数据不直接使用IO实例,改为使用 ...
- 4.5 .net core下直接执行SQL语句并生成DataTable
.net core可以执行SQL语句,但是只能生成强类型的返回结果.例如var blogs = context.Blogs.FromSql("SELECT * FROM dbo.Blogs& ...
- javascript 练习示例(一)
confirm 点确定返回true,点取消返回false prompt 点确定返回用户输入的字符串,点取消返回null 判断奇偶性 var isOdd = prompt('请输入你得的数字'); if ...