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 (a1a2, … , 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) {

         sort(candidates.begin(),candidates.end());

         vector<vector<int> > result;

         vector<int> tmp;

         backtracking(result,candidates,target,tmp,,);

         return result;

     }

     void backtracking(vector<vector<int> > &result,vector<int> &candidates, int &target,vector<int> tmp, int sum,int index)

     {

         if(sum==target)

         {

             result.push_back(tmp);

             return;

         }

         else

         {

             int sum0=sum;

             //注意index

             for(int i=index;i<candidates.size();i++)

             {

                 tmp.push_back(candidates[i]);

                 sum=sum0+candidates[i];

                 if(sum>target)

                 {

                     break;

                 }

                 backtracking(result,candidates,target,tmp,sum,i);

                 tmp.pop_back();

             }            

         }        

     }

 };

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

  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 III(middle)

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

  3. 【leetcode】Combination Sum (middle)

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

  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. Java 集合类详解(含类图)

    0.参考文献 此图中蓝色为抽象类.深红色表示接口(Arrays除外).绿色表示具体容器类 1.java集合类图 1.1 1.2 上述类图中,实线边框的是实现类,比如ArrayList,LinkedLi ...

  2. Yii2 实现修改密码功能

    YII2对密码加密生成的结果是不同的,即用相同的初始密码在不同时间得到的加密结果不同,所以我们不能用常用的方法去验证密码是否正确(将密码加密后与数据库中的密码相比较).YII2有自己的加密以及密码验证 ...

  3. Cocos2d-X3.0 刨根问底(一)----- 概览

    罗嗦几句,本系列文章记录了小鱼(本人)自学Cocos2D-X的整个过程,主要从分析Cocos2D-x的源码方式来学习Cocos2d-x这样一个优秀的游戏引擎架构,本着不但要知其然还要知其所以然的学习态 ...

  4. oracle基本语句

    ALTER TABLE SCOTT.TEST RENAME TO TEST1--修改表名 ALTER TABLE SCOTT.TEST RENAME COLUMN NAME TO NAME1 --修改 ...

  5. 34.Android之资源文件res里drawable学习

    我们经常看到android工程资源文件res下drawable如ldpi.mdpi.hdpi.xhdpi.xxhdpi文件,今天我们学习了解下. (1)drawable-hdpi里面存放高分辨率的图片 ...

  6. poj 3233 矩阵快速幂+YY

    题意:给你矩阵A,求S=A+A^1+A^2+...+A^n sol:直接把每一项解出来显然是不行的,也没必要. 我们可以YY一个矩阵: 其中1表示单位矩阵 然后容易得到: 可以看出这个分块矩阵的左下角 ...

  7. configure new linux

    vim   http://www.cnblogs.com/wswang/p/5088078.html zsh  sh -c "$(curl -fsSL https://raw.github. ...

  8. IOS基础之 (七) 分类Category

    一 Category 分类:Category(类目,类别) (OC有) 命名:原来的类+类别名(原来的类名自动生成,只要写后面的类别名,一般以模块名为名.比如原来类 Person,新建分类 Ct,新建 ...

  9. 利用Arraylist输入学生的成绩,求出平均分和总分。

    Console.WriteLine("请输入学生人数:"); int n=int.Parse(Console.ReadLine()); ArrayList arr= new Arr ...

  10. 字符串匹配的Boyer-Moore算法 详解 加 C# 实现

    上一篇文章,我介绍了KMP算法. 但是,它并不是效率最高的算法,实际采用并不多.各种文本编辑器的"查找"功能(Ctrl+F),大多采用Boyer-Moore算法. Boyer-Mo ...