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 ...
随机推荐
- 通过网络方式安装linux的五种方法
在线观看:http://video.sina.com.cn/v/b/43086503-1443650204.html http://video.sina.com.cn/v/b/43095530-144 ...
- knowledge
- SQL Server常用元数据函数
元数据函数 1.获取数据库标识符DB_ID DB_ID函数用于获取当前数据库的唯一ID(int数据类型),数据库ID用于服务器上唯一区分书库. 语法结构: DB_ID (['database_name ...
- 第三百四十一天 how can I 坚持
不好,有点肚子疼,凉肚子了. 今天晚上回来看了个电影<聚焦>,貌似明白了一个道理,任何一份职业,只要认识到了它的价值,那就好好干. 计划又放在脑门后了,上班又闲扯了一天.老季公司招人,让我 ...
- Mac下安装eclipse+python+pydev+numpy+matplotlib
*本人亲测是成功的安装过程 1.更新Mac系统默认低版本的python2.7.请参见这篇文章:http://jingyan.baidu.com/article/14bd256e39b63dbb6d26 ...
- c++一些问题总结
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
- Android NDK调试C++源码(转)
[原创文章,转载请保留或注明出处,http://download.csdn.net/download/bigmaxim/5474055] 1. 相关软件 adt-bundle-windows-x86. ...
- python 递归遍历文件夹
#!/usr/bin/python import os.path def readXmls(folder): #三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字 for ...
- C#中的Collection 1
Collection定义 Collection是个关于一些变量的集合,按功能可分为Lists,Dictionaries,Sets三个大类. Lists:是一个有序的集合,支持index look up ...
- UI:target-action设计模式、手势识别器
⼀.target/action设计模式 ⼆.代理设计模式 三.UIImageView 四.⼿势识别器 target/action设计模式 耦合是衡量⼀个程序写的好坏的标准之⼀, 耦合是衡量模块与模块之 ...