回溯法 leetcode题解 Combination Sum 递归法
题目大意:给出一个数组,用这些数组里的元素去凑一个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 递归法的更多相关文章
- [LeetCode 题解] Combination Sum
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a se ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- 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 ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [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 ...
- 从Leetcode的Combination Sum系列谈起回溯法
在LeetCode上面有一组非常经典的题型--Combination Sum,从1到4.其实就是类似于给定一个数组和一个整数,然后求数组里面哪几个数的组合相加结果为给定的整数.在这个题型系列中,1.2 ...
随机推荐
- MATLAB在三维坐标中显示图片 并 使得图片部分透明
要画一个光路图,本来可以用proe,但是鼠标不好用,有些操作也忘了,用MATLAB画了个.下面是用到的图片. 但是三维坐标中显示彩色图片的目标没有搞定,做了个灰度图,然后用仿射程序将彩色图片贴到了二维 ...
- 阿里云ECS安装的redis服务器,用java代码去连接报错。
import redis.clients.jedis.Jedis; /** * Hello world! * */ public class App { public static void main ...
- https证书的验证过程与生成方法
1.简洁的解释: 1.服务器 用RSA生成公钥和私钥2.把公钥放在证书里发送给客户端,私钥自己保存3.客户端首先向一个权威的服务器检查证书的合法性,如果证书合法,客户端产生一段随机数,这个随机数就作为 ...
- 必须熟练掌握的150个Linux命令
- Hash 迭代程序构造器要求字符串参数--错误解决
报错提示: ERROR: Hash 迭代程序构造器要求字符串参数,位置: 行 56 列 23.ERROR: DATA STEP 组件对象失败.在“EXECUTION”阶段中止.NOTE: 由于出错,S ...
- CRM 总是弹出登录窗口
最近测试机总是会出现登录窗口,也能正常进入系统,但是会反复出现. 环境:CRM2016 问题:总是弹出登录窗口 解决方法:
- Java - 24 Java 封装
Java 封装 在面向对象程式设计方法中,封装(英语:Encapsulation)是指,一种将抽象性函式接口的实作细节部份包装.隐藏起来的方法. 封装可以被认为是一个保护屏障,防止该类的代码和数据被外 ...
- CS229 6.14 Neurons Networks Restricted Boltzmann Machines
1.RBM简介 受限玻尔兹曼机(Restricted Boltzmann Machines,RBM)最早由hinton提出,是一种无监督学习方法,即对于给定数据,找到最大程度拟合这组数据的参数.RBM ...
- SpringBoot项目启用本地Tomcat
1.修改pom.xml文件,配置<packaging>war</packaging>,使其发布的时候打包成war包 <groupId>com.owlforest&l ...
- java8实战:filter的简单使用
<JAVA8实战>中的例子 要实现的功能:通过Apple的color或weight属性,对List<Apple>进行筛选. 1.首先定义com.owl.entity.Apple ...