回溯法 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 ...
随机推荐
- nginx 1.14.0 配置部署 thinkphp 5.1
开始接触NGINX,配置tp5配了半天,找不到具体原因,于是用网上搜索到的配置复制粘贴搞定. 感谢 https://blog.csdn.net/qq_36431213/article/details/ ...
- 成功设置open live writer
mark一下,哈哈哈 https://www.cnblogs.com/chrisrockdl/
- windows gitbook pdf
1.安装 nodejs 下载地址:https://nodejs.org/download/release/v6.9.2/node-v6.9.2-x64.msi 执行安装 配置环境变量:C:\Progr ...
- 对mysql事务提交、回滚的错误理解
一.起因 begin或者START TRANSACTION开始一个事务 rollback事务回滚 commit 事务确认 人们对事务的解释如下:事务由作为一个单独单元的一个或多个SQL语句组成,如果其 ...
- tp5 计算两个日期之间相差的天数
//两个日期之间相差的天数 function diffBetweenTwoDays ($day1, $day2) { $second1 = strtotime($day1); $second2 = s ...
- Vue学习记录(二)
一.指令 指令是Vue.js中一个重要的特性,主要提供了一种机制将数据的变化映射为DOM行为.当数据变化时,指令会依据设定好的操作对DOM进行修改,这样就可以只关注数据的变化,而不用去管理DOM的变化 ...
- pyautogui控制鼠标键盘自动填写数据
import os import pyautogui import time, os import pyperclip # 复制 pyautogui.FAILSAFE = False class Au ...
- [Unity插件]Lua行为树(十一):组合节点Parallel
Parallel节点类似Sequence节点,不同在于Parallel会每帧执行所有的节点.当所有节点返回成功时返回成功,当其中一个节点返回失败时,返回失败并且结束所有的子节点运行. 例如说,给Seq ...
- Oracle 命令
net start oracleserviceorcl 启动服务 sql>shutdown 关闭数据库 sql>startup 打开数据库 sql> sel ...
- transport error 202: bind failed: 地址已在使用
tomcat启动报错是因为:在catalina.sh中设置了调试启动参数 编辑catalina.sh全局搜索下 address= 去掉或者改一下address端口号,重启tomcat 另一种情况可能是 ...