题目大意:给出一个数组,用这些数组里的元素去凑一个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. document.write的用处!

    document.write是JavaScript中对document.open所开启的文档流(document stream操作的API方法,它能够直接在文档流中写入字符串,一旦文档流已经关闭,那d ...

  2. 如何进行CodeReview

    一.代码规范的要点 代码规范主要分为风格规范与设计规范两大类: 1.代码风格规范 主要是文字上的规定,看似表面文章,实际上非常重要. 具体有如下几个方面: (1)缩进 (2)行宽 (3)断行/空白行 ...

  3. Django简介及Django项目的创建详述

    Django简介 Django是一个开源的Web应用框架,由Python写成.但是,百度百科中讲它采用了MVC框架模式,其实这个解释不准确. 确切的讲,Django的模式是:路由控制+MTV模式.所谓 ...

  4. PostgreSQL 之 yum安装 postgis 插件

    版本说明: CentOS7.5 + PostgreSQL 10.5 参考资源: https://www.postgresql.org/download/linux/redhat/ http://dow ...

  5. MongoDB集群搭建之主从模式

    单机搭建 #创建docker持久化数据目录 [root@docker ~]# mkdir -p /root/application/program/mongodb/data/master-slaveM ...

  6. pytho 单例模式

    单例模式存在的目的是保证当前内存中仅存在单个实例,避免内存浪费!!! (程序如果并发量大的话,内存里就会存在非常多功能上一模一样的对象.存在这些对象肯定会消耗内存,对于这些功能相同的对象可以在内存中仅 ...

  7. 性能测试day01_性能基本概念

    其实第一次接触性能是15年的时候,懵懵懂懂的被领导拉去做第一次做性能压测,如今有机会重新听一下云层大大讲解性能,于是打算以此博客记录下整个学习的过程,如若有不同意见者可以在下面留言指出,也欢迎大家一起 ...

  8. ubantu 与Windows 资源共享

    Linux与Windows共享文件夹之samba的安装与使用(Ubuntu为例)   作者:@gzdaijie本文为作者原创,转载请注明出处:http://www.cnblogs.com/gzdaij ...

  9. spring揭密学习笔记(3)-spring ioc容器:掌管大局的IoC Service Provider

    1.IOC service Provider的概念.IoC Service Provider在这里是一个抽象出来的概念,它可以指代任何将IoC场景中的业务对象绑定到一起的实现方式.它可以是一段代码,也 ...

  10. springboot工程的添加方式

    1.将spring boot做为父工程 <parent> <groupId>org.springframework.boot</groupId> <artif ...