题目描述

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

candidates 中的数字可以无限制重复被选取。

说明:

  • 所有数字(包括 target)都是正整数。
  • 解集不能包含重复的组合。

示例 1:

输入: candidates = [2,3,6,7], target = 7,
所求解集为:
[
[7],
[2,2,3]
]

示例 2:

输入: candidates = [2,3,5], target = 8,
所求解集为:
[
  [2,2,2,2],
  [2,3,3],
  [3,5]
]

解题思路

考虑用回溯法解题。首先将数组从小到大排序,然后从第一个数字开始遍历,若该数字不大于当前目标值,则将其加入到结果数组中,然后把目标值减去当前数字,并从当前数字开始向后递归寻找下一个满足上述条件的数字。若到某一步为止目标值为0,则将当前结果数组加入到集合中。每个数字向后遍历完之后,将其从结果数组中去除,从下一个数字开始继续寻找,直到走到数组末尾或者没有不大于目标值的数。

代码

 class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
sort(candidates.begin(),candidates.end());
vector<vector<int>> res;
vector<int> temp;
if(candidates.size())
combi(candidates,target,temp,res,);
return res;
}
void combi(vector<int>& candidates, int target, vector<int>& temp, vector<vector<int>>& res, int start){
if(target==)
res.push_back(temp);
else{
int i=start;
while(i<candidates.size()&&candidates[i]<=target){
temp.push_back(candidates[i]);
combi(candidates, target-candidates[i], temp, res, i);
temp.pop_back();
i++;
}
}
}
};

LeetCode 39. 组合总和(Combination Sum)的更多相关文章

  1. Java实现 LeetCode 39 组合总和

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

  2. [LeetCode] 39. 组合总和

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

  3. [leetcode] 39. 组合总和(Java)(dfs、递归、回溯)

    39. 组合总和 直接暴力思路,用dfs+回溯枚举所有可能组合情况.难点在于每个数可取无数次. 我的枚举思路是: 外层枚举答案数组的长度,即枚举解中的数字个数,从1个开始,到target/ min(c ...

  4. Leetcode 39 40 216 Combination Sum I II III

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

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

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

  6. [Swift]LeetCode377. 组合总和 Ⅳ | Combination Sum IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  7. LeetCode——39. 组合总和

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

  8. leetcode 39 组合总和 JAVA

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

  9. leetcode 39. 组合总和(python)

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

随机推荐

  1. SQL----Group By and Having

    合计函数 (比如 SUM) 常常需要添加 GROUP BY 语句. GROUP BY 语句 GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组. SQL GROUP BY 语法 ...

  2. 关于redis的几件小事(十)redis cluster模式

    redis cluster是redis提供的集群模式. 1.redis cluster的架构 ①可以有多个master node,每个master node 都可以挂载多个slave node. ②读 ...

  3. JavaScript快速排序法实现数组排序

    大致分三步: 1.找基准(一般是以中间项为基准) 2.遍历数组,小于基准的放在left,大于基准的放在right 3.递归 var arr = [10,8,6,9,1,7,1,13,5,1,9]; / ...

  4. CentOS7部署kettle

    去官网下载kettle, 或者百度网盘下载(nnnk),解压到目录/opt/service/, 解压后的目录是data-integration kettle需要java环境才能运行,因此要安装Java ...

  5. Nginx如何配置反向代理

    server { listen 80; server_name 代理域名; location / { proxy_pass 应用域名:应用端口; proxy_set_header Host $host ...

  6. oracle存储过程临时表

    接到一个以前领导的需求,说的大概意思是: 如果能关联上就取关联上的最大值更新到表里,没有关联上的就取原来的值. 写一个存储过程,这正好用到了临时表,上网查询,用的太乱了,特别记录. 准备阶段 创建PD ...

  7. 测试Random类nextInt()方法连续两次结果一样的概率

    public static void main(String[] args) { int count = 0; int a = 0; Random r = new Random(); while (t ...

  8. 服务命令(systemctl的使用)

    常用的service与systemctl命令的对比 应用举例: ●start:开启服务 ●stop:停止服务 ●status:参数来查看服务运行情况 ●restart:重新加载服务 应用举例·: #启 ...

  9. mysql explain解析一 extra中的using index,using where,using index condition

    1.简单介绍 using index 和using where只要使用了索引我们基本都能经常看到,而using index condition则是在mysql5.6后新加的新特性,我们先来看看mysq ...

  10. Vi编辑器中查找替换

    1 第一个是替换当前行的yesterday为all 第二个是替换全局的 yesterday 为all :s/yesterday/all :s/yesterday/all/g