这道题想到的就是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 组合总和的更多相关文章

  1. [Swift]LeetCode39. 组合总和 | Combination Sum

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  2. LeetCode39.组合总和 JavaScript

    给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选 ...

  3. 216. 组合总和 III

    216. 组合总和 III 题意 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. 解集不能包含重复的 ...

  4. LeetCode 中级 - 组合总和II(105)

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...

  5. Leetcode 377.组合总和IV

    组合总和IV 给定一个由正整数组成且不存在重复数字的数组,找出和为给定目标正整数的组合的个数. 示例: nums = [1, 2, 3] target = 4 所有可能的组合为: (1, 1, 1, ...

  6. Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III)

    Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III) 同类题目: Leetcode之回溯法专题-39. 组合总数(Combination Sum) Lee ...

  7. Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)

    Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...

  8. 40组合总和II

    题目:给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一 ...

  9. [LeetCode] 39. 组合总和

    题目链接 : https://leetcode-cn.com/problems/combination-sum/ 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ...

随机推荐

  1. Centos安装MySQL5.6并重置密码

    数据库配置 如果用的是自带的sqllite那么数据库就可以不动 安装MySQL5.6数据库 这里强烈建议用使用5.6, 5.7版本的数据库遇见了很多BUG 安装MySQL wget http://de ...

  2. nginx解决浏览器跨域问题

    1.跨域问题 浏览器出于安全方面的考虑,只允许与本域下的接口交互.不同源的客户端脚本在没有明确授权的情况下,不能读写对方的资源. 例如访问www.test1.com 页面, 返回的文件中需要ajax向 ...

  3. H2数据库启动提示8082端口被占用

    The Web Console server could not be started. Possible cause: another server is already running at ht ...

  4. C#Socket编程(一)简介

    一.TCP与UDP简介 https://blog.csdn.net/subin_iecas/article/details/80289513 二.单播.多播.广播 https://blog.csdn. ...

  5. python 中 open与with open 的区别

    读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘, ...

  6. Redis之哨兵机制(五)

    什么是哨兵机制 Redis的哨兵(sentinel) 系统用于管理多个 Redis 服务器,该系统执行以下三个任务: ·        监控(Monitoring): 哨兵(sentinel) 会不断 ...

  7. C# 继承(3)持续更新

    类继承 和 接口继承 类继承        一个类型派生于一个基类行,它拥有该基类型的所有成员字段和函数. 接口继承     一个类型继承函数的签名,不需要实现代码. 多重继承 一个类派生自多个类.多 ...

  8. opengles reference card

    https://www.khronos.org/files/opengles31-quick-reference-card.pdf https://www.khronos.org/opengles/s ...

  9. (二)线程Thread中的方法详解

    1.start() start()方法的作用讲得直白点就是通知"线程规划器",此线程可以运行了,正在等待CPU调用线程对象得run()方法,产生一个异步执行的效果.通过start( ...

  10. win10 LTSC 2019 激活

    win 10 打开终端 1.slmgr -ipk M7XTQ-FN8P6-TTKYV-9D4CC-J462D 2.slmgr -skms kms.03k.org 3.slmgr -ato 4. slm ...