LeetCode 39. 组合总和(Combination Sum)
题目描述
给定一个无重复元素的数组 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)的更多相关文章
- Java实现 LeetCode 39 组合总和
39. 组合总和 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字 ...
- [LeetCode] 39. 组合总和
题目链接 : https://leetcode-cn.com/problems/combination-sum/ 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ...
- [leetcode] 39. 组合总和(Java)(dfs、递归、回溯)
39. 组合总和 直接暴力思路,用dfs+回溯枚举所有可能组合情况.难点在于每个数可取无数次. 我的枚举思路是: 外层枚举答案数组的长度,即枚举解中的数字个数,从1个开始,到target/ min(c ...
- 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 ...
- [Swift]LeetCode39. 组合总和 | Combination Sum
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- [Swift]LeetCode377. 组合总和 Ⅳ | Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- LeetCode——39. 组合总和
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选 ...
- leetcode 39 组合总和 JAVA
题目: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制 ...
- leetcode 39. 组合总和(python)
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选 ...
随机推荐
- 帝国cms 【反馈案例】 代码
<form name='feedback' method='post' enctype='multipart/form-data' action='/e/enews/index.php' ons ...
- Layedit 编辑页面赋值
1.编辑页面 $("[name=Experience]").val(data.Experience);//直接赋值然后再进行build experience = layedit.b ...
- Linux和Windows双系统下Windows系统插入耳机没有声音
我的笔记本装了Windows7和Debian双系统后,在Windows7下,插入耳机竟然没有声音. 按常规思路分析:首先考虑是耳机问题还是笔记本电脑问题.确定耳机没问题后问题就在笔记本身上了.而问题在 ...
- 月薪 30K Java 程序员,需要掌握哪些技术?
转载自:Java3y 1-5年的Java程序员,薪资区间大致是在15-25K左右,那有没有可能提前达到30K的薪资呢?有人说这只能是大企业或者互联网企业工程师才能拿到.也许是的,小公司或者非互联网企业 ...
- stm32 development
1.www.st.com st官网 2.www.stmcu.com.cn st中文网 3.www.stmcu.org.cn st中文社区
- 架构师成长之路5.3-Saltstack配置管理(State状态模块)
点击架构师成长之路 架构师成长之路5.3-Saltstack配置管理(State状态模块) 配置管理工具: Pupper:1. 采用ruby编程语言:2. 安装环境相对较复杂:3.不支持远程执行,需要 ...
- k8s管理pod资源对象(上)
一.容器于pod资源对象 现代的容器技术被设计用来运行单个进程时,该进程在容器中pid名称空间中的进程号为1,可直接接收并处理信号,于是,在此进程终止时,容器即终止退出.若要在一个容器中运行多个进程, ...
- mysql更改列属性的一些用法
更改mysql 主键属性 alter table rbac_auth change column id id int auto_increment
- Python 操作 MySQL 数据库Ⅳ
执行事务 事务机制可以确保数据一致性. 事务应该具有4个属性:原子性.一致性.隔离性.持久性.这四个属性通常称为ACID特性. 原子性(atomicity).一个事务是一个不可分割的工作单位,事务中包 ...
- javascript中constructor指向问题
首先用一个例子指出来constructor存在形式. function Fruit(){ } var f=new Fruit(); console.log(f.constructor);//打印出Fr ...