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. poj 3111 K Best (二分搜索之最大化平均值之01分数规划)

    Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...

  2. .NET 面试题(1)

    1.简述 private. protected. public. internal 修饰符的访问权限. private:私有成员,在类的内部才能访问 protected:保护成员,在该类内部和继承本类 ...

  3. python高级编程之元类(第3部分结束)

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #元编程 #new-style类带来了一种能力,通过2个特殊方法(_ ...

  4. sql求和涉及到null值

    SQL ISNULL().NVL().IFNULL() 和 COALESCE() 函数 请看下面的 "Products" 表: P_Id ProductName UnitPrice ...

  5. android jni (5)——Field & Method --> Accessing Mehtod

    在java编程语言中有非静态成员函数和静态成员函数,JNI允许我们访问到java中的成员函数,然后再jni中调用,这里我就来举例说明在jni中是如何做到的. 我们先在java中定义2个成员函数,一个非 ...

  6. .NET(C#):分析IL中的if-else,while和for语句并用Emit实现

    这是一篇关于IL和反射Emit的文章(所以不喜欢IL或者Emit的就没必要往下看了),要求读者对IL和Emit工作原理较了解.所有分析IL均在Visual Studio 2010 SP1下编译生成.( ...

  7. mycat源码分析

    http://www.cnblogs.com/fernandolee24/p/5196367.html

  8. js 随手记

    var name = 'frog' function hello(){ alert(name); // undefined var name = 'bbc'; } 在javascript中,函数是可以 ...

  9. C#使用seleium实现一个自动登录器

    1.http://docs.seleniumhq.org/ 下载seleium包 2.新建一个C#项目,比如控制台,引用seleium包中的dll using System; using System ...

  10. 一篇文章讲清楚android ImageView.ScaleType

    2016-01-10 刚开始android编程的时候, 关于ImageView.ScaleType网络上好多, 说实话没看懂. 本文就是为了讲清楚这个, 有用的话转走, 请注明原地址和作者. 典型的代 ...