[LeetCode 题解] Combination Sum
前言
【LeetCode 题解】系列传送门: http://www.cnblogs.com/double-win/category/573499.html
1.题目描述
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
Note:
- All numbers (including target) will be positive integers.
- Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
- The solution set must not contain duplicate combinations.
For example, given candidate set 2,3,6,7 and target 7,
A solution set is: [7]
[2, 2, 3]
2. 题意
给定一个候选集合,集合中的元素以非递减方式存放。给定一个目标值T。
输出所有唯一的组合。
3. 思路
由于不能出现重复元组,所以我们先将candidate set进行排序,然后规定比新插入的元素不能比当前元素小。
采用DFS思想即可。
4: 解法
class Solution {
private:
vector<int> ans;
vector<vector<int> > ret;
public:
void DFS(int start,vector<int> &candidates, int target){
if(target==0){
ret.push_back(ans);
return;
} for(int i=start;i<candidates.size();++i){
if(target <candidates[i]) return;
ans.push_back(candidates[i]);
DFS(i,candidates,target-candidates[i]);
ans.pop_back();
}
}
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
ans.clear();
ret.clear();
if(!target || !candidates.size()) return ret;
sort(candidates.begin(),candidates.end());
DFS(0,candidates,target);
return ret;
}
};
![]() |
作者:Double_Win 出处: http://www.cnblogs.com/double-win/p/3896010.html 声明: 由于本人水平有限,文章在表述和代码方面如有不妥之处,欢迎批评指正~ |
[LeetCode 题解] Combination Sum的更多相关文章
- 回溯法 leetcode题解 Combination Sum 递归法
题目大意:给出一个数组,用这些数组里的元素去凑一个target.元素可以重复取用. 感觉对这种题目还是生疏的.脑子里有想法,但是不知道怎么表达出来. 先记录下自己的递归法.应该还可以用循环实现. 回溯 ...
- 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 ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 40. Combination Sum II 组合之和 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] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- 从Leetcode的Combination Sum系列谈起回溯法
在LeetCode上面有一组非常经典的题型--Combination Sum,从1到4.其实就是类似于给定一个数组和一个整数,然后求数组里面哪几个数的组合相加结果为给定的整数.在这个题型系列中,1.2 ...
随机推荐
- 五、配置jenkins定时构建或上游job触发构建
我们之前说的都是通过检测github是否有push动作,即代码是否有更新,一旦检测到push动作就出发jenkins构建: 但是除了这种方式,我们可能还会需要定时进行构建,比如在每天的凌晨1:00构建 ...
- 如何清除svn的账号缓存信息(solaris)
如果我们不小心输入svn账号错误的话,后面就一直提示认证失败,不能checkout代码. 这个是因为svn把你输入的账号进行了缓存. 如果我们想重新输入新的账号,必须要清除缓存 svn存储账号的目录在 ...
- LeetCode题解 #12 Integer to Roman
题目大意:给定数字,将其转化为罗马数字的形式 罗马数字其实只有 I V X L C D M 这几种形式,其余均为组合的,去百度了解一下就ok. 所以首先想到的就是,将个.十.百.千位的数字构造出来,然 ...
- springboot整合最新版dubbo以及dubbo-admin的安装
一.安装前准备 由于dubbo被阿里捐献给了apache,这次安装admin时,参考网上的资料,地址还是停留在之前的链接,踩了不少坑,这里记录下. dubbo-admin下载地址: 地址一:https ...
- Mysql Replication 主从同步
简介: Mysql 的主从同步功能,这种解决方案是企业很常见的一种.常用于备份数据库,当客户端操作主库时,主库会产生binlog日志文件, 从库通过复制主库的binlog日志文件,然后解析成相应的 S ...
- Memcache 内存对象缓存系统
简介: Memcached 是一个高性能的分布式内存存储对象缓存系统,用于动态 WEB 应用以减轻数据库负载. 它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据库驱动网站的速度. ...
- 从零开始构建一个的asp.net Core 项目(一)
最近突发奇想,想从零开始构建一个Core的MVC项目,于是开始了构建过程. 首先我们添加一个空的CORE下的MVC项目,创建完成之后我们运行一下(Ctrl +F5).我们会在页面上看到“Hello W ...
- 团队作业4Alpha冲刺
仓库地址:https://gitee.com/ILoveFunGame/game_strategy_network.git 第一天 2018/6/13 1.1 今日完成任务情况以及遇到的问题. 1.1 ...
- 【LA4043 训练指南】蚂蚁 【二分图最佳完美匹配,费用流】
题意 给出n个白点和n个黑点的坐标,要求用n条不相交的线段把他们连接起来,其中每条线段恰好连接一个白点和一个黑点,每个点恰好连接一条线段. 分析 结点分黑白,很容易想到二分图.其中每个白点对应一个X结 ...
- 刷题向》一道简单的思路题BZOJ1800(EASY+)
这道题其实并不难,主要原因是数据范围很小,当然数据如果大来也可以优化,但重点是在做的时候用的思路很通用, 所以本题是一道思想题(当然思想也不难) 标题里的“+”体现在一些边界处理中. 直接甩题目 De ...