题目大意:给出一个数组,用这些数组里的元素去凑一个target。元素可以重复取用。

感觉对这种题目还是生疏的。脑子里有想法,但是不知道怎么表达出来。

先记录下自己的递归法。应该还可以用循环实现。

回溯:罗列出所有的不重复的可能组合,每次判断:如果超出,放弃。如果不够,继续添加元素。如果刚好,存起来。

比如:a = [2 3 6 7]  target = 7

第一次,分成以下几个树去继续分叉:    2  3  6  7

第二次:

2 2  2 3  2 6  2 7  

3 3  3 6  3 7  

6 6  6 7   

7  满足了,直接存起来。

第三次继续分叉

class Solution {
public:
// a 是给出的可选数组,start表示当前分支只能从start开始取数。last表示已经取的一些数。needed表示还差多少。
void back_track(vector<int>& a ,int start,vector<int>last, int needed)
{
if(needed == ) ans.push_back(last);  //刚好满足,就存起来
if(needed < ) return;    //数组都是正数。

      //加入下一个合法数,继续流程
for(int i = start;i < a.size();i++)
{
vector<int> tmp(last);
tmp.push_back(a[i]);
back_track(a,i,tmp,needed - a[i]);
}
} vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
for(int i = ; i < candidates.size(); i++)
{
vector<int> vec;
vec.push_back(candidates[i]);  //放入一个数,启动回溯
back_track(candidates,i,vec,target - candidates[i]);
}
return ans;
} private:
vector<vector<int>> ans;
};

回溯法 leetcode题解 Combination Sum 递归法的更多相关文章

  1. [LeetCode 题解] Combination Sum

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a se ...

  2. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  3. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  4. [LeetCode] 40. Combination Sum II 组合之和 II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  5. [LeetCode] 377. Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  6. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  7. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  8. [LeetCode] 216. Combination Sum III 组合之和 III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  9. 从Leetcode的Combination Sum系列谈起回溯法

    在LeetCode上面有一组非常经典的题型--Combination Sum,从1到4.其实就是类似于给定一个数组和一个整数,然后求数组里面哪几个数的组合相加结果为给定的整数.在这个题型系列中,1.2 ...

随机推荐

  1. CSS自定义滚动条样式

    原文地址:http://www.qianduan.net/css-custom-scroll-bar-style/ 相信很多人都遇到过在设计中自定义滚动条样式的情景,之前我都是努力说服设计师接受浏览器 ...

  2. SEO之HTML代码优化

    原文地址:http://www.admin5.com/article/20081128/117821.shtml   一.文档类型(DOCTYPE) XHTML1.0有三种DOCTYPE: 1.过渡型 ...

  3. T-SQL 事务2

    启用事务完成转账存储过程 use StudentManager go if exists(select * from sysobjects where name='usp_TransferAccoun ...

  4. WebSocket 启用压缩

    m_client.Compression = CompressionMethod.Deflate;

  5. Linux安装jsvc,及Linux服务开发

    在linux上以服务的方式启动java程序,需要提前安装jsvc.linux是利用daemon(jsvc)构建java守护进程. 编译 daemon 安装JSVC 1 下载文件,http://comm ...

  6. U3D学习003——编辑器使用

    1.skybox 原来的render setting 在2017版本中是lighting标签environment中设置: 或者在摄像机对象中添加skybox组件,进行设置. 2.6张图实现自定义sk ...

  7. delphi打开项目提示unable to find resource on dll projects

    用记事本打开*.dof文件, 把这行[Resource DLL Projects]及它的所属的内容删除就行了.

  8. 让WordPress支持google AMP

    1.关于AMP 在移动互联网的时代,尽管网站响应式设计可以满足多屏(pc.手机.ipad等)浏览,但google在2015年10月推出了更快移动页面访问速度的技术-Accelerated Mobile ...

  9. spring boot controller设置 @Transactional 不回滚的解决办法

    @Transactional @ApiOperation(value = "添加一个用户信息") @RequestMapping(value = "/create&quo ...

  10. Solr Date类型的哪些你不得不了解的细节

    我们先来看看Solr日期类型的一些内幕,然后讨论一下Solr日期类型存在的一些问题,最后我们看看怎么解决现存的问题.概述 DateField 在Solr4.x之前,我们只有DateField,这类型现 ...