LeetCode39 Combination Sum
题目:
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. (Medium)
Note:
- All numbers (including target) will be positive integers.
- 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]
]
分析:
先排个序,用DFS搜索,每个数可选可不选,然后在start > end或者candidates[start] > target后就return。
恰好candidates[start] = target满足时添加到结果中。
代码:
class Solution {
private:
vector<vector<int>>result;
void dfs(int start, int end, const vector<int>& candidates, int target, vector<int>& internal) {
if (start > end) {
return;
}
if (candidates[start] == target) {
internal.push_back(candidates[start]);
result.push_back(internal);
internal.pop_back();
return;
}
if (candidates[start] > target) {
return;
}
dfs(start + , end, candidates, target, internal);
internal.push_back(candidates[start]);
dfs(start, end, candidates, target - candidates[start], internal);
internal.pop_back();
}
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
int end = candidates.size() - ;
vector<int> internal;
dfs(, end, candidates, target, internal);
return result;
}
};
LeetCode39 Combination Sum的更多相关文章
- Leetcode39.Combination Sum组合总和
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选 ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] Combination Sum 组合之和
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- 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 ...
- LeetCode:Combination Sum I II
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- Combination Sum | & || & ||| & IV
Combination Sum | Given a set of candidate numbers (C) and a target number (T), find all unique comb ...
- 【leetcode】Combination Sum II
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
随机推荐
- C#获取文件的绝对路径
要在c#中获取路径有好多方法,一般常用的有以下五种: //获取应用程序的当前工作目录. String path1 = System.IO.Directory.GetCurrentDirectory() ...
- Time vs Story Points Estimation [转]
One of the most common questions we get is whether to estimate in time or points. It seems like poin ...
- Using Boost Libraries in Windows Store and Phone Applications
Using Boost Libraries in Windows Store and Phone Applications RATE THIS Steven Gates 18 Jul 2014 5:3 ...
- HDU 2042 不容易系列之二 [补6.24] 分类: ACM 2015-06-26 20:40 9人阅读 评论(0) 收藏
不容易系列之二 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- Web Service实现分布式服务的基本原理
简单的说, 就是客户端根据WSDL 生成 SOAP 的请求消息, 通过 HTTP 传输方式(也可以是其它传输方式, 如 FTP 或STMP 等,目前 HTTP 传输方式已经成为 J2EE Web Se ...
- 获取和设置localStorage
东钿金融服务平台 用户第一次访问页面出现,引导步骤,起初一直使用cookie,但是cookie一直不稳定 今天老大说改用localStorage 于是乎百度,查了一篇博客 http://www.cnb ...
- Ubuntu 搭建PHP开发环境
Ubuntu确实很好玩.有喜欢的命令行,简洁的界面,不同于Window要的感觉.偶尔换换环境工作,学习Linux的思维方式,是一种不错的做 法.之前也折腾过Ubuntu,不过,因为网络的问题,一直没有 ...
- C#之 HashSet(临时笔记,未参考资料,请慎重)
HashSet是一个集合,类似于DataSet,但是其主要用途是用来存放同一种类型的元素(string.row.table等),如果添加的元素跟定义时初始的类型不一致,就会直接编译失败. 例如: Ha ...
- 如何选择PDA的操作系统? Android OR WINCE?
PDA(Personal Digital Assistant),又称为掌上电脑,可以帮助我们完成在移动中工作,学习,娱乐等.按使用来分类,分为工业级PDA和消费品PDA.当我们需要购买PDA的时候, ...
- 关于.NET邮件的收发问题总结
转载:http://www.cnblogs.com/ustbwuyi/archive/2007/05/28/762581.html //取数据库中邮件信息中的最大发送时间,即最近接收到的一封邮件的时间 ...