LeetCode:Combination Sum I II
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
Note:
- All numbers (including target) will be positive integers.
- Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
- The solution set must not contain duplicate combinations.
For example, given candidate set 2,3,6,7
and target 7
,
A solution set is: [7]
[2, 2, 3]
简单的回溯法(递归实现).
比如对于数组3,2,6,7,target = 7,对数组排序得到[2,3,6,7]
1、第1个数字选取2, 那么接下来就是解决从数组[2,3,6,7]选择数字且target = 7-2 = 5
2、第2个数字选择2,那么接下来就是解决从数组[2,3,6,7]选择数字且target = 5-2 = 3
3、第3个数字选择2,那么接下来就是解决从数组[2,3,6,7]选择数字且target = 3-2 = 1
4、此时target = 1小于数组中的所有数字,失败,回溯,重新选择第3个数字
5、第3个数字选择3,那么接下来就是解决从数组[2,3,6,7]选择数字且target = 3-3 = 0
6、target = 0,找到了一组解,继续回溯寻找其他解
需要注意的是:如果数组中包含重复元素,我们要忽略(因为每个数字可以选择多次,如果不忽略的话,就会产生重复的结果)。貌似oj的测试集数组中都不包含重复的数字
class Solution {
private:
vector<vector<int> > res;
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
sort(candidates.begin(), candidates.end());//为了输出结果递增,因此先对数组排序
vector<int> tmpres;
helper(candidates, 0, target, tmpres);
return res;
} //从数组candidates[index,...]寻找和为target的组合
void helper(vector<int> &candidates, const int index, const int target, vector<int>&tmpres)
{
if(target == 0)
{
res.push_back(tmpres);
return;
}
for(int i = index; i < candidates.size() && target >= candidates[i]; i++)
if(i == 0 || candidates[i] != candidates[i-1])//由于每个数可以选取多次,因此数组中重复的数就不用考虑
{
tmpres.push_back(candidates[i]);
helper(candidates, i, target - candidates[i], tmpres);
tmpres.pop_back();
}
}
};
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination.
Note:
- All numbers (including target) will be positive integers.
- Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
- The solution set must not contain duplicate combinations.
For example, given candidate set 10,1,2,7,6,1,5
and target 8
,
A solution set is:
[1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]
和上一题差不多,只是每个元素只能选一次。
由于有重复元素的存在,比如数组为[1(1),1(2),2,3],target = 6. 可能出现重复结果1(1),2,3 和 1(2),2,3 本文地址
我们可以如下处理:如果数组中当前的数字出现重复,在前面重复了k次,且临时结果数组中也包含了k个当前数字,那么当前的数字可以选择;否则就不选择当前数字
class Solution {
private:
vector<vector<int> >res;
public:
vector<vector<int> > combinationSum2(vector<int> &candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<int> tmpres;
helper(candidates, 0, target, tmpres, 0);
return res;
} //从数组candidates[index,...]寻找和为target的组合,times为前一个数字candidates[index-1]重复出现的次数
void helper(vector<int> &candidates, const int index, const int target, vector<int>&tmpres, int times)
{
if(target == 0)
{
res.push_back(tmpres);
return;
}
for(int i = index; i < candidates.size() && target >= candidates[i]; i++)
{
if(i > 0 && candidates[i] == candidates[i-1])times++;
else times = 1;
if(times == 1 || (tmpres.size() >= times-1 && tmpres[tmpres.size()-times+1] == candidates[i]))
{
tmpres.push_back(candidates[i]);
helper(candidates, i+1, target - candidates[i], tmpres, times);
tmpres.pop_back();
}
}
}
};
还有一种方法是,在每个子问题的数组中,重复的数字都不选择,这种更简洁
class Solution {
private:
vector<vector<int> >res;
public:
vector<vector<int> > combinationSum2(vector<int> &candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<int> tmpres;
helper(candidates, 0, target, tmpres);
return res;
} //从数组candidates[index,...]寻找和为target的组合
void helper(vector<int> &candidates, const int index, const int target, vector<int>&tmpres)
{
if(target == 0)
{
res.push_back(tmpres);
return;
}
for(int i = index; i < candidates.size() && target >= candidates[i]; i++)
{
if(i > index && candidates[i] == candidates[i-1])continue;//当前子问题中,重复数字都不选择
tmpres.push_back(candidates[i]);
helper(candidates, i+1, target - candidates[i], tmpres);
tmpres.pop_back();
}
}
};
【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3802647.html
LeetCode:Combination Sum I II的更多相关文章
- LeetCode: Combination Sum I && II && III
Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- Leetcode 39 40 216 Combination Sum I II III
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- LeetCode: Combination Sum II 解题报告
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- 子集系列(二) 满足特定要求的子集,例 [LeetCode] Combination, Combination Sum I, II
引言 既上一篇 子集系列(一) 后,这里我们接着讨论带有附加条件的子集求解方法. 这类题目也是求子集,只不过不是返回所有的自己,而往往是要求返回满足一定要求的子集. 解这种类型的题目,其思路可以在上一 ...
- [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] Combination Sum 组合之和
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- LeetCode Combination Sum III
原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...
随机推荐
- DOM浏览器文档模型
DOM浏览器文档模型 1.对XHM/XHTML 编写成DOM文档 可形成动态网页 2.'*'获取所有节点 GetElementsByTagName():获取所有的标签名的集合--需要在js中使用win ...
- windows server 2008 - 创建域和本机用户
/* * ===================================================================================== * Filenam ...
- php 消息实时推送(反ajax推送)
入口文件index.html <!DOCTYPE HTML> <html> <head> <title>反ajax推送</title> &l ...
- Sql Server 相关错误问题及解决方法
1.首当其冲是登陆问题, SQL Server 2008选择Windows身份验证无法登录 (Microsoft Sql Server,错误:18456) 就是在连接SQL Server 2008时, ...
- 今天在Mac机器上使用了Flex Builder编辑了一个源代码文件,保存后使用vim命令去打开时发现系统自动在每一行的结尾添加了^M符号,其实^M在Linux/Unix中是非常常见的,也就是我们在Win中见过的/r回车符号。由于编辑软件的编码问题,某些IDE的编辑器在编辑完文件之后会自动加上这个^M符号。看起来对我们的源代码没有任何影响,其实并不然,当我们把源代码文件Check In到svn之类
今天在Mac机器上使用了Flex Builder编辑了一个源代码文件,保存后使用vim命令去打开时发现系统自动在每一行的结尾添加了^M符号,其实^M在Linux/Unix中是非常常见的,也就是我们在W ...
- [原] XAF 如何将数据库中Byte array图片显示出来
问题比较简单,直接上代码. private Image _Cover; [Size(SizeAttribute.Unlimited), ValueConverter(typeof(ImageValue ...
- xps 文件操作笔记
1. 在 Silverlight 显示XPS文件,参考:http://azharthegreat.codeplex.com/ 2. Word,Excel, PPT 文件转换为XPS: 参考一(老外写的 ...
- javascript中的splice方法介绍&示例
javascript 中的 splice 方法很强大,它可以用于插入.删除或替换数组的元素. 下面来一一介绍! 删除:用于删除元素,两个参数,第一个参数(要删除第一项的位置),第二个参数(要删除的项数 ...
- [OpenCV] 1、读取图片
>_<" 安装及配置请看http://www.cnblogs.com/zjutlitao/p/4042074.html >_<" 这篇是一个简单的在VS20 ...
- iOS——Swift开发中的单例设计模式(摘译,非原创)
最近在开发一个小的应用,遇到了一些Objective-c上面常用的单例模式,但是swift上面还是有一定区别的,反复倒来倒去发现不能按常理(正常的oc to swift的方式)出牌,因此搜索了一些帖子 ...