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]

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

  1. class Solution {
  2. private:
  3. vector<vector<int>> res;
  4. vector<int> temp;
  5. int tempsum=;
  6.  
  7. public:
  8. void combinationSum(vector<int>& candidates, int target,vector<int>::iterator initer,int tempsum)
  9. {
  10.  
  11. if(initer==candidates.end()||tempsum>target)
  12. return ;
  13. if(tempsum==target)
  14. {
  15. // temp.push_back(*initer);
  16. res.push_back(temp);
  17. return ;
  18. }
  19.  
  20. for(vector<int>::iterator iter=initer;iter!=candidates.end();iter++)
  21. {
  22. temp.push_back(*iter);
  23. combinationSum(candidates,target,iter, tempsum+*iter);
  24. temp.pop_back();
  25.  
  26. }
  27. }
  28. vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
  29. sort(candidates.begin(),candidates.end());
  30. vector<int>::iterator initer=candidates.begin();
  31. combinationSum(candidates,target,initer,);
  32. return res;
  33.  
  34. }
  35. };

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]

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

  1. class Solution {
  2. private:
  3. vector<vector<int>> res;
  4. vector<int> temp;
  5. public:
  6. void combinationSum(vector<int>& candidates, int target,vector<int>::iterator initer,int tempsum)
  7. {
  8. if(tempsum==target)
  9. {
  10. if(find(res.begin(),res.end(),temp)==res.end())
  11. res.push_back(temp);
  12. return ;
  13. }
  14. if(initer==candidates.end()||tempsum>target)
  15. return ;
  16. for(vector<int>::iterator iter=initer;iter!=candidates.end();iter++)
  17. {
  18. temp.push_back(*iter);
  19. combinationSum(candidates,target,iter+, tempsum+*iter);
  20. temp.pop_back();
  21.  
  22. }
  23. }
  24. vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
  25. sort(candidates.begin(),candidates.end());
  26. vector<int>::iterator initer=candidates.begin();
  27. combinationSum(candidates,target,initer,);
  28. return res;
  29.  
  30. }
  31. };
  32.   

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. Emmet缩写语法

    缩写语法 Emmet 使用类似于 CSS 选择器的语法描述元素在生成的文档树中的位置及其属性. 元素 可以使用元素名(如 div 或者 p)来生成 HTML 标签.Emmet 没有预定义的有效元素名的 ...

  2. 爬虫实例——爬取淘女郎相册(通过selenium、PhantomJS、BeautifulSoup爬取)

    环境 操作系统:CentOS 6.7 32-bit Python版本:2.6.6 第三方插件 selenium PhantomJS BeautifulSoup 代码 # -*- coding: utf ...

  3. DOM基本代码一

    dom学习基本代码第一部分 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> < ...

  4. python读书笔记-django架站过程总结(from the django book)

    django架站过程总结:1.django-admin startproject store2.store这个project的目录下有:__init__,manage,setting,urls3.se ...

  5. linux tar指定文件到指定目录

    项目需求:从远处拷贝压缩文件到本地后并解压,解压后的部分文件拷贝并重命名到其他目录 需求拆分:1.拷贝文件 2.解压文件 3.批量重命名 步骤一查看:http://www.cnblogs.com/dd ...

  6. How to Disable System Integrity Protection (rootless) in OS X El Capitan

    mac在10.11之后增加了一个功能,号称"System Integrity Protection, often called rootless",有了这个功能,以下目录的东西都不 ...

  7. Nginx+Tomcat关于Session的管理

    前言 Nginx+Tomcat对Session的管理一直有了解,但是一直没有实际操作一遍,本文从最简单的安装启动开始,通过实例的方式循序渐进的介绍了几种管理session的方式. nginx安装配置 ...

  8. 2015/9/4 Python基础(8):映射和集合类型

    Python里唯一的映射类型是字典.映射类型对象里,hash值(key)和指向的对象(值)是一对多的关系.字典对象是可变的,这一点上很像列表,它也可以存储任意个数任意类型的Python对象,其中包括容 ...

  9. MySQL增删改查之查询

    (7)范围查询select * from car where price>40 and price<60   --查询价格在40-60之间的select * from car where ...

  10. embed标签 阻止点击事件 让父元素处理点击事件

    由于规定页面显示的PDF文件要有固定大小,使得页面风格统一 最开始发现了CSS样式pointer-events 写出如下代码,在360急速浏览器急速模式中访问可在点击PDF控件时可跳转页面 <a ...