回溯法 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 ...
随机推荐
- 【ZZ】技能表合集
技能表 http://w.itcodemonkey.com/tag/373.html 1 当一名黑客应该学什么?来看看安全工程师技能表 2 软件测试工程师技能表 3 大数据.数据挖掘技能表 4 C/C ...
- [UE4]网络游戏重点思考
一.哪些逻辑放服务器,哪些逻辑放客户端? 二.它们之间怎么通信?会不会覆盖? 三.服务器应该做哪些验证?(防止外挂)
- 2018年最新PHP面试题
面试之前多看看公司的资料,可以看出面试的公司主要做什么,电商,数据库,php函数,sql的优化,接口,session和cookie等经常会问到,都是必问之题,这其中有一部分题目摘抄自网络,回答也不错 ...
- Centos 7 下, 安装odoo 10
1. Centos在虚拟机中, 最小化安装, 网络连接选择的是 桥接模式, 安装完成后, 是不能直接上网的, 输入root 和密码, 登录进去, 然后执行: [root@localhost ~]# v ...
- linux下的环境变量配置
方法一: 方法二:
- 第6章 静态路由和动态路由(4)_OSPF动态路由协议
6. OSPF动态路由协议 6.1 OSPF协议(Open Shortest Path First,OSPF开放式最短路径优先协议) (1)通过路由器之间通告链路的状态来建立链路状态数据库,网络中所有 ...
- Javascript-string-Array
1.得到数组里重复的值 function getRepeat(ar){ //数组排序 var ary = ar.sort(); //创建装重复值的新数组 var newArr = new Array( ...
- Docker使用札记 - Dockerfile指令
ARG ARG跟ENV都可以定义变量,不同在于ARG在构建期结束时是销毁,而ENV定义的是系统中的环境变量,不会在构建结束时销毁,在以后的构建中直接使用. 当ARG和ENV定义相同名称的变量时,ENV ...
- alias with parameter,linux
alias demoAlias1='_(){ git checkout -b $1; command2;}; _'
- 聊聊 cursor鼠标样式
在前端开发中,很多时候需要对页面的某些元素做鼠标样式的处理,比如button一般用pointer , 文本区一般用text......等等. 今天咱就来聊聊 这个经常用到的 cursor 属性 cur ...