leetcode39 组合总和

这道题想到的就是dfs,在累加的和大于或等于target时到达递归树的终点。
代码如下:
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
int r=candidates.size()-1;
vector<vector<int>> res;
for(int i=r;i>=0;i--){
vector<int> tmp;
int sum=0;
dfs(candidates, tmp, res, target, i, sum);
}
return res;
}
void dfs(vector<int>& candidates, vector<int>& tmp, vector<vector<int>>& res, int target,int id,int& sum){
sum+=candidates[id];
tmp.push_back(candidates[id]);
if(sum>=target){
if(sum==target) res.push_back(tmp);
sum-=candidates[id];
tmp.pop_back();
return;
}
for(int i=id;i>=0;i--){
dfs(candidates,tmp,res,target,i,sum);
}
tmp.pop_back();
sum-=candidates[id];
}
};
leetcode39 组合总和的更多相关文章
- [Swift]LeetCode39. 组合总和 | Combination Sum
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- LeetCode39.组合总和 JavaScript
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选 ...
- 216. 组合总和 III
216. 组合总和 III 题意 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. 解集不能包含重复的 ...
- LeetCode 中级 - 组合总和II(105)
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...
- Leetcode 377.组合总和IV
组合总和IV 给定一个由正整数组成且不存在重复数字的数组,找出和为给定目标正整数的组合的个数. 示例: nums = [1, 2, 3] target = 4 所有可能的组合为: (1, 1, 1, ...
- Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III)
Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III) 同类题目: Leetcode之回溯法专题-39. 组合总数(Combination Sum) Lee ...
- Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)
Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...
- 40组合总和II
题目:给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一 ...
- [LeetCode] 39. 组合总和
题目链接 : https://leetcode-cn.com/problems/combination-sum/ 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ...
随机推荐
- 11.SSH整合
由于自己学习的版本比较落后,这里就不总结了 在我这个版本整合的过程中的几点问题: 1.在web.xml的配置过程中: <!-- 如果使用的是load获取数据,在jsp页面申请取得数据时才真正的执 ...
- ASP.NET IHttpModule IHttpHandler IHttpHandlerFactory 拦截请求
先来看看代码,拦截所有Http请求类.下面包含了两种类的集成 IHttpModule IHttpHandlerFactory using System; using System.Collection ...
- 【坑】maven编码配置
错误环境: maven 3.5.0 idea 2017.1.1 错误原因: 由于没有设置统一编码,导致与其他同事开发过程中出现乱码问题 解决方案: 在maven的 pom配置中properties节点 ...
- Codeforces 845G Shortest Path Problem?
http://codeforces.com/problemset/problem/845/G 从顶点1dfs全图,遇到环则增加一种备选方案,环上的环不需要走到前一个环上作为条件,因为走完第二个环可以从 ...
- Delphi 线程的基本概念
- vue2.0+webpack+vuerouter+vuex+axios构建项目基础
前言 本文讲解的是vue2.0+webpack+vuerouter+vuex+axios构建项目基础 步骤 1.全局安装webpack,命令 npm install webpack -g 注意,web ...
- MNPR--造福人类的人值得被感激
https://artineering.io/research/MNPR/ https://mnpr.artineering.io https://pdfs.semanticscholar.org/0 ...
- spark数据倾斜处理
spark数据倾斜处理 危害: 当出现数据倾斜时,小量任务耗时远高于其它任务,从而使得整体耗时过大,未能充分发挥分布式系统的并行计算优势. 当发生数据倾斜时,部分任务处理的数据量过大,可能造成内存不足 ...
- SpringDataJpa 分页查询
第一种方式 实体类 Student import javax.persistence.*; import java.io.Serializable; @Entity @Table(name=" ...
- Mysql-sql行转列
原始数据如下图所示:(商品的销售明细)date=业务日期:Item=商品名称:saleqty=销售数量 -- 建立测试数据(表)create table test (Date varchar(10), ...