题目大意:给出一个数组,用这些数组里的元素去凑一个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. Python首次安装后运行报错(0xc000007b)的解决方法

    最近在安装完Python后运行发现居然报错了,错误代码是0xc000007b,于是通过往上查找发现是因为首次安装Python缺乏VC++库的原因,下面通过这篇文章看看如何解决这个问题吧.   错误提示 ...

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

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

  3. JVM调优原理

    JVM堆栈 栈是运行时的单位,而堆是存储的单位. 栈解决程序的运行问题,即程序如何执行,或者说如何处理数据:堆解决的是数据存储的问题,即数据怎么放.放在哪儿. 在Java中一个线程就会相应有一个线程栈 ...

  4. HibernateUtil工具类的使用

    为了简化代码的重复性,使用HibernateUtil工具类对Hibernate有关的代码进行整合 主要实现有,getSessionFactory(),getSession(),closeSession ...

  5. Unity中进程间通信——使用Protobuf-net序列化与反序列化

    基于ProtoBuf协议实现网络传输(上) Protobuf 全称Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,很适合做数据存储或 RPC 数据交换格 ...

  6. solr学习之域的管理与中文分析器配置

    该文使用  Centos6.5 64 位    solr4.10.3   IK-Analyzer中文分析器 一.solr域 在solr中域的概念与lucene中域的概念相同,数据库的一条记录或者一个文 ...

  7. Solr——从postgresql数据库导入数据

    1,配置准备 本文的前提是你已经配置好了solr,并新创建了一个core,我们下面都会按照前一篇文章中的core_demo为基础开始 2,修改soreconfig.xml 在soreconfig.xm ...

  8. Cmder - 在右键菜单添加"Cmder Here"

    使用命令行或终端工具的时候都有一个让我们觉得麻烦的问题,就是需要cd很多目录达到目标位置.在可视化操作系统下面我们一般都是已经处在目标目录了,这时需要执行某些命令如: python test.py 现 ...

  9. 报错500 DEFAULT_INCOMPATIBLE_IMPROVEMENTS

    freemarker整合springMVC报错如下:org.springframework.beans.factory.BeanCreationException: Error creating be ...

  10. 【Selenium-WebDriver自学】Selenium-IDE用户扩展(七)

    ==================================================================================================== ...