I:

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 (a1a2, … , 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]

这里用到了回溯的方法,回溯其实就是一种深度优先搜索算法,相当于在整个解空间搜索问题的解,类似于穷举法,但是与穷举法的区别在于回溯法用到了剪枝,使得许多不是问题的解提前排出了,减少搜索的次数和时间。

class Solution {
private:
vector<vector<int>> res;
vector<int> temp;
int tempsum=; public:
void combinationSum(vector<int>& candidates, int target,vector<int>::iterator initer,int tempsum)
{ if(initer==candidates.end()||tempsum>target)
return ;
if(tempsum==target)
{
// temp.push_back(*initer);
res.push_back(temp);
return ;
} for(vector<int>::iterator iter=initer;iter!=candidates.end();iter++)
{
temp.push_back(*iter);
combinationSum(candidates,target,iter, tempsum+*iter);
temp.pop_back(); }
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
sort(candidates.begin(),candidates.end());
vector<int>::iterator initer=candidates.begin();
combinationSum(candidates,target,initer,);
return res; }
};

II:

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 (a1a2, … , 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]

感觉自己代码写的有点复杂,不过好歹是通过了,接下来需要慢慢的把代码写简洁点,通用点。

class Solution {
private:
vector<vector<int>> res;
vector<int> temp;
public:
void combinationSum(vector<int>& candidates, int target,vector<int>::iterator initer,int tempsum)
{
if(tempsum==target)
{
if(find(res.begin(),res.end(),temp)==res.end())
res.push_back(temp);
return ;
}
if(initer==candidates.end()||tempsum>target)
return ;
for(vector<int>::iterator iter=initer;iter!=candidates.end();iter++)
{
temp.push_back(*iter);
combinationSum(candidates,target,iter+, tempsum+*iter);
temp.pop_back(); }
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(),candidates.end());
vector<int>::iterator initer=candidates.begin();
combinationSum(candidates,target,initer,);
return res; }
};
  

Combination Sum I&&II(经典的回溯算法题)的更多相关文章

  1. 大公司面试经典数据结构与算法题C#/Java解答

    几个大公司(IBM.MicroSoft and so on)面试经典数据结构与算法题C#解答 1.链表反转 我想到了两种比较简单的方法 第一种是需要开一个新的链表,将原链表的元素从后到前的插入到新链表 ...

  2. 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 ...

  3. LeetCode:Combination Sum I II

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  4. 子集系列(二) 满足特定要求的子集,例 [LeetCode] Combination, Combination Sum I, II

    引言 既上一篇 子集系列(一) 后,这里我们接着讨论带有附加条件的子集求解方法. 这类题目也是求子集,只不过不是返回所有的自己,而往往是要求返回满足一定要求的子集. 解这种类型的题目,其思路可以在上一 ...

  5. LeetCode: Combination Sum I && II && III

    Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...

  6. combination sum(I, II, III, IV)

    II 简单dfs vector<vector<int>> combinationSum2(vector<int>& candidates, int targ ...

  7. HYSBZ - 2038经典莫队算法题

    无修改的莫队 emmm莫队的几条性质,必须是离线的,复杂度是n*sqrt(n) 就是通过预处理查询区间,然后从(l,r)转移到(ll,rr),这样的复杂度是曼哈顿距离,即abs(l-ll)+abs(r ...

  8. 3、回溯算法解题套路框架——Go语言版

    前情提示:Go语言学习者.本文参考https://labuladong.gitee.io/algo,代码自己参考抒写,若有不妥之处,感谢指正 关于golang算法文章,为了便于下载和整理,都已开源放在 ...

  9. 算法题C#

    几个大公司(IBM.MicroSoft and so on)面试经典数据结构与算法题C#解答 1.链表反转 我想到了两种比较简单的方法 第一种是需要开一个新的链表,将原链表的元素从后到前的插入到新链表 ...

随机推荐

  1. PowerDesigner 技巧【2】

    去掉Oracle生成的SQL创建语句中的双引号 用powerdesigner导出orale数据库的建表sql时,默认会给表名和字段名加上双引号,如下图: 这样给操作数据库带来很大的不便,解决的办法是设 ...

  2. HDU4022 Bombing STL

    Bombing Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Su ...

  3. HDU4612:Warm up(缩点+树的直径)

    Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Su ...

  4. 无线局域网中RADIUS协议原理与实现

    转载自:http://blog.csdn.net/jinhill/article/details/5901042 摘要  RADIUS协议是一个被广泛应用于网络认证.授权和计费的协议.本文在介绍了RA ...

  5. swiper 、css3制作移动端网站,折叠导航

    swiper .css3制作移动端网站,折叠导航 前几天公司要更新改版移动端的官网,由于网站本身没有多少内容,所以设计师就做成了整屏滑动的样子,起初我并没有看设计稿就一口答应了,拿到手后发现了几个问题 ...

  6. String StrigBuffer StringBuilder 浅解

    1.String是最基本的字符串类,用于表示字符串. 特点:对象内容不可变,但可以通过指向不同的对象来“表示”不同的内容. 使用场景:如果不涉及到内容改变,可以使用String. 注意:如果想将Str ...

  7. C++ 指针常见用法小结

    1. 概论 2.指针基础 3. 指针进阶 4. 一维数组的定义与初始化 5. 指针和数组 6. 指针运算 7. 多维数组和指针 8. 指针形参 9. 数组形参 10. 返回指针和数组 11. 结语   ...

  8. SPOJ 104 HIGH - Highways

    HIGH - Highways http://www.spoj.com/problems/HIGH/ In some countries building highways takes a lot o ...

  9. C11简洁之道:tupe元祖

    tuple元组是一个固定大小不同类型的值的集合,是泛化的std::pair.我们也可以把它当作一个通用的结构体来使用,不需要创建结构体有获取结构体特征,在某些情况可以取代结构体,使程序更简洁.直观. ...

  10. Tomcat报错:Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/JFreeChartTest]]

    最好把项目移除,然后在tomcat的webapps发布路径下也把项目文件删掉,重新部署就好了,原因是可能在tomcat的remove覆盖中以前的文件有所保留导致冲突,亲测有效