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 ...
随机推荐
- 远控软件VNC攻击案例研究
欢迎大家给我投票: http://2010blog.51cto.com/350944 本文出自 "李晨光原创技术博客" 博客,谢绝转载!
- HD2086A1 = ?
Problem Description 有如下方程:Ai = (Ai-1 + Ai+1)/2 - Ci (i = 1, 2, 3, .... n). 若给出A0, An+1, 和 C1, C2, .. ...
- C# Common Keyword
[C# Common Keyword] 1.abstract Use the abstract modifier in a class declaration to indicate that a c ...
- HDU 4618 Palindrome Sub-Array (2013多校2 1008 暴力)
Palindrome Sub-Array Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Oth ...
- HDU 2147 kiki's game (简单博弈,找规律)
kiki's game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 40000/1000 K (Java/Others)Total ...
- HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- jQuery在updatepanel中使用造成内存泄露
wijmo用户反馈了一个RadialGauge控件内存泄露的bug,采用chrome监控内存使用情况,发现明显的内存泄露,在前面的文章中我就发现了jQuery内存泄露的问题,这次再次发现此问题,自然就 ...
- 利用 Heritrix 构建特定站点爬虫
http://www.ibm.com/developerworks/cn/opensource/os-cn-heritrix/
- 常见Java基础知识(较长)
1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部分细节.抽象包括两个方面,一是过程抽象, ...
- jquery实现的个性网站首页 详细信息
http://download.csdn.net/detail/a757956132/9305419 实现效果: 1:导航效果(兼容IE) 2:网站换肤 3:模块展开和关闭 4:新闻滚动 5:超链接文 ...