题目大意:给出一个数组,用这些数组里的元素去凑一个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. 客户端负载均衡Ribbon之二:Loadbalance的源码

    Load Balance负载均衡是用于解决一台机器(一个进程)无法解决所有请求而产生的一种算法. 像nginx可以使用负载均衡分配流量,ribbon为客户端提供负载均衡,dubbo服务调用里的负载均衡 ...

  2. 云中树莓派(3):通过 AWS IoT 控制树莓派上的 Led

    云中树莓派(1):环境准备 云中树莓派(2):将传感器数据上传到AWS IoT 并利用Kibana进行展示 云中树莓派(3):通过 AWS IoT 控制树莓派上的Led 云中树莓派(4):利用声音传感 ...

  3. pj1--学生信息管理系统

    1.根据班上的情况做一个班级学生信息管理系统.包含功能有每日签到.学分管理.个人信息管理 2.要求:用winform+序列化(本地化)的技术实现,以教师机做服务器,要有文件保存.读取.还要有上传头像功 ...

  4. selector的小箭头去除

    selector的小箭头去除 .not-arrow{ -webkit-appearance:none; -moz-appearance:none; appearance:none; /*去掉下拉箭头* ...

  5. optparse模块解析命令行参数的说明及优化

    一.关于解析命令行参数的方法 关于“解析命令行参数”的方法我们一般都会用到sys.argv跟optparse模块.关于sys.argv,网上有一篇非常优秀的博客已经介绍的很详细了,大家可以去这里参考: ...

  6. RHEL7安装配置VNC

    RHEL7安装配置VNC 作者:Eric 微信:loveoracle11g 安装配置VNC服务程序 [root@zhouwanchun yum.repos.d]# cd ~ [root@zhouwan ...

  7. a++ 与 ++a 的运算

    var a=5: b=a++和b=++a的区别: 前者是先赋值,再自加,即b=a:a=a+1: //结果b=5,a=6 后者是先自加,再赋值,即a=a+1;b=a;  //结果a=6,b=6

  8. html 网页生产pdf文件

    在nuget中安装组件 Install-Package CPechkin https://www.nuget.org/packages/CPechkin/ 根据html生产pdf文件 using Sy ...

  9. 05 Linux系统下的用户以及用户权限管理(权限管理介绍、用户管理、常见命令介绍)

    这一节我们介绍Linux的用户以及权限管理的前半段,包括:1.权限管理介绍: 2.用户管理: 3.常见命令 权限管理介绍 权限管理: 为了访问计算机资源,我们需要对其进行授权才能访问,根据什么东西来进 ...

  10. Hadoop2.6.5集群搭建

    一. Hadoop的分布式模型 Hadoop通常有三种运行模式:本地(独立)模式.伪分布式(Pseudo-distributed)模式和完全分布式(Fully distributed)模式.安装完成后 ...