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. 【图论】tarjan的离线LCA算法

    百度百科 Definition&Solution 对于求树上\(u\)和\(v\)两点的LCA,使用在线倍增可以做到\(O(nlogn)\)的复杂度.在NOIP这种毒瘤卡常比赛中,为了代码的效 ...

  2. VS 2013 with update安装失败(kb2829760)解决方案

    update过程中遇到kb2829760补丁无法更新而导致vs安装失败的解决方法: 1.安装KB2829760: 2.安装KB2829760中文语言包: 3.安装VS2013 with update. ...

  3. Django问题 TypeError: __init__() missing 1 required positional argument: 'on_delete'

    问题:在执行python manage.py makemigrations learning_logs时,系统会报错,提示:TypeError: __init__() missing 1 requir ...

  4. MongoDB:删除操作

    一. 根据查询条件删除文档 查询 id=1 的所有文档 test:PRIMARY> db.test_1.find({id:1}) { "_id" : ObjectId(&qu ...

  5. bnuoj25660 Two Famous Companies

    题目链接:https://www.bnuoj.com/v3/problem_show.php?pid=25660 这个二分真的是烧脑QAQ,想了一晚上才懂了一个大概. 首先,整体思路是二分,直观上感受 ...

  6. UVA:11183:Teen Girl Squad (有向图的最小生成树)

    Teen Girl Squad Description: You are part of a group of n teenage girls armed with cellphones. You h ...

  7. Semaphore信号量

    一.前言 互斥锁 同时只允许一个线程更改数据,而Semaphore是同时允许一定数量的线程更改数据 ,比如厕所有3个坑,那最多只允许3个人上厕所,后面的人只能等里面有人出来了才能再进去. 二.sema ...

  8. 【Android】Android之Copy and Paste

    Android为复制粘贴提供了一个强大的基于剪切板的框架,它支持简单和复杂的数据类型,包括纯文本,复杂的数据结构,二进制流,甚至app资源文件.简单的文本数据直接存储在剪切板中,而复杂的数据则存储的是 ...

  9. 51Nod 1004 n^n末尾数字 | 快速幂

    #include "bits/stdc++.h" using namespace std; #define LL long long #define INF 0x3f3f3f3f3 ...

  10. LightOJ 1062 - Crossed Ladders 基础计算几何

    http://www.lightoj.com/volume_showproblem.php?problem=1062 题意:问两条平行边间的距离,给出从同一水平面出发的两条相交线段长,及它们交点到水平 ...