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.

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]

从给定集合里找出和为Target的数的组合,一个数可以重复使用。

结果集不能重复,且每一个结果内按升序排列。

解题思路:

这是一个很好的题目,用到了回溯法,考点跟之前那道Permutation有点点像。代码有相似之处。

每次遇到下一个数时,如果它比target大,因为nums是排序的,那么肯定匹配不上了,返回。

否则的话,将这个数push_back到中间结果里,开始递归。递归结束时需要将这个数pop出来,在这个位置继续尝试下一个数。

代码如下:

class Solution {
public:
vector<vector<int> > combinationSum(vector<int> &nums, int target) {
sort(nums.begin(),nums.end());
vector<int> temp; //中间结果
vector<vector<int>> result; //最终结果
dp(nums,,target,temp,result);
return result;
} void dp(vector<int> &nums,int start,int target,vector<int> &temp,vector<vector<int>> &result){
if(target == ){ //找到了一个合法的解
result.push_back(temp);
return ;
} for(int i = start; i < nums.size(); i++){
if(nums[i] > target){ //剪枝
return ;
}
temp.push_back(nums[i]); //往中间向量里加一个数
dp(nums,i,target - nums[i],temp,result);
temp.pop_back(); //撤销
}
}
};

【LeetCode练习题】Combination Sum的更多相关文章

  1. 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 ...

  2. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  3. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  4. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  5. [LeetCode] 40. Combination Sum II 组合之和 II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  6. [LeetCode] 216. Combination Sum III 组合之和 III

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

  7. [LeetCode] 377. Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  8. 从Leetcode的Combination Sum系列谈起回溯法

    在LeetCode上面有一组非常经典的题型--Combination Sum,从1到4.其实就是类似于给定一个数组和一个整数,然后求数组里面哪几个数的组合相加结果为给定的整数.在这个题型系列中,1.2 ...

  9. Leetcode 之 Combination Sum系列

    39. Combination Sum 1.Problem Find all possible combinations of k numbers that add up to a number n, ...

  10. [LeetCode] 377. Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

随机推荐

  1. 方案:抵御 不明SSL证书导致的 中间人攻击

    基于SSL数字证书的中间人攻击已经不是一个新技术了,但是我发现很多人并不清楚这种威胁,甚至感觉无所谓,我相信他们是由于短暂的无知蒙蔽了双眼,希望这篇文章可以让更多的人知道这种攻击方式,并清除这种网络威 ...

  2. Spoj1771-Yet Another N-Queen Problem(精确覆盖)

    Description After solving Solution to the n Queens Puzzle by constructing, LoadingTime wants to solv ...

  3. <转载>C++命名空间

    原文http://blog.csdn.net/liufei_learning/article/details/5391334 一. 为什么需要命名空间(问题提出) 命名空间是ANSIC++引入的可以由 ...

  4. hdu 5288 OO’s Sequence(计数)

    Problem Description OO has got a array A of size n ,defined a function f(l,r) represent the number o ...

  5. pyqt menu子级方向例子学习

    代码和UI文件:http://yunpan.cn/QCkXbX8mnSNke(提取码:51e1) 图片如: 代码如下: from PyQt4 import QtCore,QtGui,Qt import ...

  6. PC--CSS命名

    头:header内 容:container尾:footer导航:nav侧栏:sidebar栏目:column页 面外围控制整体布局宽度:wrapper左右中:left right center登录条: ...

  7. 为项目编写Readme.MD文件

    了解一个项目,恐怕首先都是通过其Readme文件了解信息.如果你以为Readme文件都是随便写写的那你就错了.github,oschina git gitcafe的代码托管平台上的项目的Readme. ...

  8. 国内外移动端web适配屏幕方案

    基础知识点 设备像素:设备像素又称物理像素(physical pixel),设备能控制显示的最小单位,我们可以把这些像素看作成显示器上一个个的点. iPhone5的物理像素是640X1136. PS: ...

  9. 关于Http协议(2)--转载

    原文链接:http://www.cnblogs.com/mcad/ HTTP工作原理图 请求报文 1.请求报文长什么样?  Chrome核心的请求报文 2.报文结构 3.报文头部每个字段的意义 //从 ...

  10. GSS2-Can you answer these queries II

    ---恢复内容开始--- 这道题真的是非常恶心,看题解看了半天才弄懂,而且题解上说的相当简略. 此题大意是询问去掉重复元素的最大子区间和,没有修改操作. 没有修改操作,这样就可以离线处理了. 这道题有 ...