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 ...
随机推荐
- Spark生态系统BDAS
目前,Spark已经发展成为包含众多子项目的大数据计算平台. 伯克利将Spark的整个生态系统称为伯克利数据分析栈(BDAS). 其核心框架是Spark,同时BDAS涵盖支持结构化数据SQL查询与分析 ...
- iconv 的参数问题
工作中遇到一个转码的问题,如下代码说话 void encode_convert(iconv_t& cd, const char* str, size_t str_len, std::strin ...
- HDU 5839 Special Tetrahedron (2016CCPC网络赛08) (暴力+剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5839 在一个三维坐标,给你n个点,问你有多少个四面体(4个点,6条边) 且满足至少四边相等 其余两边不 ...
- CodeForces 711B Chris and Magic Square (暴力,水题)
题意:给定n*n个矩阵,其中只有一个格子是0,让你填上一个数,使得所有的行列的对角线的和都相等. 析:首先n为1,就随便填,然后就是除了0这一行或者这一列,那么一定有其他的行列是完整的,所以,先把其他 ...
- iOS版本检测与版本升级
14年苹果官方要求所有的APP不能出现 “当前版本”字样,是因为从iOS8系统开始,你可以在设置里面设置在WiFi情况下,自动更新安装的APP.此功能大大方便了用户,但是一些用户没有开 启此项功能,因 ...
- XML与DataSet相互转换,DataSet查询
以FileShare.Read形式读XML文件: string hotspotXmlStr = string.Empty; try { Stream fileStream = new FileStre ...
- 异常:exception和error的区别
Throwable 是所有 Java 程序中错误处理的父类 ,有两种子类: Error 和 Exception . Error :表示由 JVM 所侦测到的无法预期的错误,由于这是属于 JVM ...
- 利用Notepad++插件(JSToolNpp):让JS代码格式化对齐
http://www.ycyjkj.com/post-663.html 程序员或者正在学习的同学一般都会遇到一个问题,看别人的源码,只是一行,没有分开,让人看的很别扭,也可能是作者故意这样做的,也可以 ...
- Chrysler -- CCD (Chrysler Collision Detection) Data Bus
http://articles.mopar1973man.com/general-cummins/34-engine-system/81-ccd-data-bus CCD (Chrysler Coll ...
- Binary Search
Binary Search [原文见:http://www.topcoder.com/tc?module=Static&d1=tuto ...