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. js-定时任务setInterval,setTimeout,clearInterval,clearTimeout

    setInterval()循环执行相应的方法 <script type="text/javascript"> setInterval("myInterval( ...

  2. 【bzoj3246】 Ioi2013—Dreaming

    www.lydsy.com/JudgeOnline/problem.php?id=3246 (题目链接) 题意 给出一棵不完全的树,要求在树上连最少的边使得所有点联通,并且使得两点间最大距离最小. S ...

  3. ajax提交特殊字符的处理

    前台页面用encodeURIComponent()这个js方法 rule=encodeURIComponent(rule);//对特殊字符编码 后台页面 java.net.URLDecoder url ...

  4. 设置div居中

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 初试visual studio2012的新型数据库LocalDB

    http://www.cnblogs.com/zhangran/archive/2012/08/21/2649200.html 今天在vs2012里面打开以前的mvc3项目,结果弹出警告说在vs201 ...

  6. Android实战_来电拦截专家

    1 项目演示: 2 代码演示: 1)MainActivity类代码: MainActivity类代码: package com.example.phoneinteceptor_one;import j ...

  7. [Angularjs]单页应用之分页

    写在前面 在项目中,用到单页应用的分页,当时想到使用滚动加载的方案,可是几次尝试都没配置成功,闲着无聊就弄了一个demo. 系列文章 [Angularjs]ng-select和ng-options [ ...

  8. Batman崛起之地——Gotham

    刚看完FOX美剧<Gotham>第一集,很喜欢这种充满黑暗元素的影视剧,虽然是电视剧,却有电影的紧凑感和叙事分格. Gotham虚构了一个日渐堕落的美国城市,它充斥暴力.性等各种犯罪,普通 ...

  9. asp.net缓存(转)

    转自:http://www.cnblogs.com/knowledgesea/archive/2012/06/20/2536603.html 一.缓存概念,缓存的好处.类型.            缓 ...

  10. Ubuntu如何安装secureCRT

    以前在ubuntu上安装过secureCRT,是自己按照网上的教程安装的. 电脑重装了系统之后,想在电脑上安装一个,又得去网上搜,安装完后,自己总结了一下. 1,下载secureCRT包 根据自己电脑 ...