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]

思路: 有规律的查找,避免重复。用递归得到所有的解

class Solution {
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
vector<vector<int>> ans;
if(candidates.empty())
return ans; sort(candidates.begin(), candidates.end()); //从小到大排序
recursion(ans, candidates, , target);
return ans;
} void recursion( vector<vector<int> > &ans, vector<int> candidates, int k, int target)
{
static vector<int> partans;
if(target == ) //如果partans中数字的总和已经达到目标, 压入答案
{
ans.push_back(partans);
return;
}
if(target < )
return; for(int i = k; i < candidates.size(); i++) //当前压入大于等于candidates[k]的数字
{
int sum = candidates[i];
while(sum <= target) //数字可以压入多次,只要和小于等于目标即可
{
partans.push_back(candidates[i]);
recursion(ans, candidates, i + , target - sum); //后面只压入大于当前数字的数,避免重复
sum += candidates[i];
}
while(!partans.empty() && partans.back() == candidates[i]) //状态还原
partans.pop_back();
}
}
};

其他人更短的递归,用参数来传partans. 省略了状态还原的代码,数字压入多次也采用了递归而不是循环

class Solution {
public: void search(vector<int>& num, int next, vector<int>& pSol, int target, vector<vector<int> >& result)
{
if(target == )
{
result.push_back(pSol);
return;
}
if(next == num.size() || target - num[next] < )
return; pSol.push_back(num[next]);
search(num, next, pSol, target - num[next], result);
pSol.pop_back(); search(num, next + , pSol, target, result);
} vector<vector<int> > combinationSum(vector<int> &num, int target)
{
vector<vector<int> > result;
sort(num.begin(), num.end());
vector<int> pSol;
search(num, , pSol, target, result);
return result;
}
};

其他人动态规划的代码,还没看,速度并不快, 但很短

class Solution {
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
sort(candidates.begin(), candidates.end());
vector< vector< vector<int> > > combinations(target + , vector<vector<int>>());
combinations[].push_back(vector<int>());
for (auto& score : candidates)
for (int j = score; j <= target; j++){
auto sls = combinations[j - score];
if (sls.size() > ) {
for (auto& s : sls)
s.push_back(score);
combinations[j].insert(combinations[j].end(), sls.begin(), sls.end());
}
}
return combinations[target];
}
};

【leetcode】Combination Sum (middle)的更多相关文章

  1. 【leetcode】Combination Sum II

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  2. 【leetcode】Combination Sum

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  3. 【leetcode】Combination Sum III(middle)

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  4. 【leetcode】Combination Sum II (middle) ☆

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  5. 【LeetCode】Combination Sum II(组合总和 II)

    这道题是LeetCode里的第40道题. 题目要求: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. can ...

  6. 【LeetCode】Combination Sum(组合总和)

    这道题是LeetCode里的第39道题. 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组 ...

  7. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  8. 【leetcode】907. Sum of Subarray Minimums

    题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...

  9. 【Leetcode】【Medium】Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

随机推荐

  1. 进程通信---FIFO

    管道没有名字,所以只能在具有血缘关系的进程间使用,而在无名管道发展出来的有名管道FIFO,则有路径名与之相关联,以一种特殊设备文件形式存在于文件系统中,从而允许无亲缘关系的进程访问FIFO,下面看FI ...

  2. 杭电ACM2058--The sum problem

    http://acm.hdu.edu.cn/showproblem.php?pid=2058 以为简单的穷举就完了,结果是一直Time Limit Exceeded.. 这是代码: #include ...

  3. Find the Difference

    Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...

  4. pancake的排序- 1.3 一摞烙饼的排序 《编程之美》读书笔记03

    问题:     星期五的晚上,一帮同事在希格玛大厦附近的“硬盘酒吧”多喝了几杯.程序员多喝了几杯之后谈什么呢?自然是算法问题.有个同事说:“我以前在餐馆打工,顾客经常点非常多的烙饼.店里的饼大小不一, ...

  5. 巧用Excel分列功能处理数据

    Technorati 标签: 数据处理      今天,主要工作就是处理测试数据,统计汇总成图表来显示.先来说说要求,然后给出我在折腾这堆数据中遇到的问题以及解决方法.   问题要求:       格 ...

  6. .NET中变量的类型问题

    1.JAVA和C# Byte的不同. java里一个byte取值范围是-128~127, 而C#里一个byte是0~255. 首位不同. 但是底层I/O存储的数据是一样的, 比如, 十进制的100, ...

  7. 村田噪声抑制基础教程-第一章 需要EMI静噪滤波器的原因

    1-1. 简介 EMI静噪滤波器 (EMIFIL®) 是为电子设备提供电磁噪声抑制的电子元件,配合屏蔽罩和其他保护装置一起使用.这种滤波器仅从通过连线传导的电流中提取并移除引起电磁噪声的元件.第1章说 ...

  8. Linux系统环境变量及命令

    Linux哪些我们常用,但是用的时候想不起来,这里做一个备忘录. Linux常用的变量: PATH 决定了shell将到哪些目录中寻找命令或程序 HOME 当前用户主目录 HISTSIZE 历史记录数 ...

  9. $(document).height()、$("body").height()、$(window).height()区别和联系

    前言:在此以高度为示例,宽度问题可类推.在移动端开发中,经常遇到需要把一块内容定位于底部的情况,当页面内容不满一屏时,需要设为fixed,而超过 一屏时,需要设为static随页面顶到底部,此时就需要 ...

  10. MemProof教程

    简介 MemProof(内存清道夫)是AutomatedQA出品的一款非常不错的检测内存泄漏和资源泄漏的免费调试工具,适合于WIN32平台下使用DELPHI/C++ BUILDER开发的应用程序. 利 ...