[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 ...
随机推荐
- 13_java之final|static|包|匿名对象|代码块|内部类
01final关键字概念 * A: 概述 继承的出现提高了代码的复用性,并方便开发.但随之也有问题,有些类在描述完之后,不想被继承, 或者有些类中的部分方法功能是固定的,不想让子类重写.可是当子类继承 ...
- gridView删除提示框
实现方法: 双击GridView的OnRowDataBound事件: 在后台的GridView1_RowDataBound()方法添加代码,最后代码如下所示: protected void GridV ...
- sql中经度维度计算距离
------------------------------------------创建一个方法---------------------------------------------------- ...
- 从邮件原理来看 postfix和docecot
好早好早以前计算机网络老师就教了说,邮件嘛,就三个协议smtp,imap,pop3. smtp 用来发邮件,imap,pop3用来收邮件.噢?是么.难道没有发现这句话有非常多的漏洞,根本就不能说清楚这 ...
- Linux实战教学笔记42:squid代理与缓存实践(一)
第1章 Squid介绍 1.1 缓存服务器介绍 缓存服务器(英文意思cache server),即用来存储(介质为内存及硬盘)用户访问的网页,图片,文件等等信息的专用服务器.这种服务器不仅可以使用户可 ...
- python要点之III
[python要点之III] 1.实现交换. 在C/C++中,交换两个变量,需要2个变量,tmp=x;x=y;y=tmp;. 在python中,交换两个变量可以这么写:x,y=y,x. 2.is&am ...
- sql解决主键冲突
在数据插入的时候,假设主键对应的值已经存在,则插入失败!这就是主键冲突.当主键存在冲突(duplicate key)的时候,可以选择性的进行处理,即忽略.更新或者替换. 1.忽略 insert ign ...
- Nginx源码完全注释(6)core/murmurhash
下面是摘自 Google Code 的 Murmurhash 开源项目主页上的 Murmurhash2,Nginx 就是采用的这个. uint32_t MurmurHash2 ( const void ...
- Ros学习——Cmakelists.txt文件解读
1.过程 .Required CMake Version (cmake_minimum_required) //CMake 需要的版本 .Package Name (project()) //#定义工 ...
- Cloudstack4.2之改变数据卷容量的大小(Resize Data Volumes)
下图标注了这个功能在cloudstack4.2 UI中的位置 在cloudstack中是通过磁盘服务来设定卷的大小的.管理员可以设置相应的磁盘服务以供用户来使用.为了增强系统的灵活性,方便最终用户的使 ...