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. 「LibreOJ NOIP Round #1」七曜圣贤

    题目啰嗦:支持三个操作: 不可重复集合:1.加入一个数 2.删除一个数 3.恢复目前最早的一次删除的数 操作可能不合法,每次有效操作之后求集合的mex(最小没有出现过的数) 50组数据+1e6,必须O ...

  2. POI 10.28

    [POI2015]KUR 不考虑构造原串再匹配 考虑开始位置满足什么条件才能匹配. 显然,开始位置确定,后面的字符都确定了. 而且,a,n互质,所以必然能遍历n的剩余系,从不同位置开始,初始的a*s+ ...

  3. Android源码4.4.4_r1下载和编译

    系统:ubuntu 16.04.2 TLS 1.源码下载: sudo apt-get install curl curl https://storage.googleapis.com/git-repo ...

  4. 仅此一文让你明白ASP.NET MVC 之View的显示

    有些人要问题,为什么我要学框架?这里我简单说一下,深入理解一个框架,给你带来最直接的好处: 使用框架时,遇到问题可以快速定位,并知道如何解决: 当框架中有些功能用着不爽时,你可以自由扩展,实现你想要的 ...

  5. How to speed up insertion performance in PostgreSQL

    Disable any triggers on the table Drop indexes before starting the import, re-create them afterwards ...

  6. lightoj 1214

    lightoj 1214 Large Division  (大数除法) 链接:http://www.lightoj.com/volume_showproblem.php?problem=1214 题意 ...

  7. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  8. 如何根据域名来得到对应的IP

    如何根据域名来得到对应的IP呢? windows下打开cmd窗口,然后ping.如下图: 这样就可以看到IP了. 如何查看自己电脑对应的IP? 当通过代理上网时,可能无法通过网络连接信息查看自己电脑的 ...

  9. 可能是国内最火的开源项目 —— C/C++ 篇

    程序员们,在北上广你还能买房吗? >>>   推荐阅读: 可能是最火的开源项目 -- Java 篇 可能是国内最火的开源项目 -- PHP 篇 可能是国内最火的开源项目 -- Pyt ...

  10. [洛谷P2852] [USACO06DEC]牛奶模式Milk Patterns

    洛谷题目链接:[USACO06DEC]牛奶模式Milk Patterns 题目描述 Farmer John has noticed that the quality of milk given by ...