https://oj.leetcode.com/problems/combination-sum/

给一列数,3 2 1 3 3 8 7 9 ,每个数可以重复多次,给target 7, 问可以加起来得7的所有组合。

递归,类似深搜的思想。

  1. class Solution {
  2. public:
  3. vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
  4. vector<vector<int> > ans;
  5. if(candidates.size()==)
  6. return ans;
  7.  
  8. //remove duplicates
  9. sort(candidates.begin(),candidates.end());
  10. vector<int>::iterator itr = unique(candidates.begin(),candidates.end());
  11. candidates.resize(itr - candidates.begin());
  12. if(candidates[]>target)
  13. return ans;
  14.  
  15. //remove the elements great than target
  16. int len = candidates.size();
  17. while(len>= && candidates[len-] > target)
  18. len--;
  19. candidates.resize(len);
  20.  
  21. vector<int> ansPiece;
  22. combinationSum(candidates,ans,target,,ansPiece);
  23.  
  24. return ans;
  25. }
  26. void combinationSum(vector<int> & candidates,vector<vector<int> > &ans,int target,int pivot,vector<int> ansPiece)
  27. {
  28. if(target == )
  29. {
  30. ans.push_back(ansPiece);
  31. return;
  32. }
  33. if( target < ||pivot == candidates.size())
  34. return;
  35.  
  36. int i = ;
  37. while(target - i>=)
  38. {
  39. if(i != )
  40. ansPiece.push_back(candidates[pivot]);
  41. combinationSum(candidates,ans,target - i,pivot+,ansPiece);
  42.  
  43. i = i + candidates[pivot];
  44. }
  45. }
  46. };

LeetCode OJ--Combination Sum **的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  10. Leetcode 之 Combination Sum系列

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

随机推荐

  1. hadoop核心组件概述及hadoop集群的搭建

    什么是hadoop? Hadoop 是 Apache 旗下的一个用 java 语言实现开源软件框架,是一个开发和运行处理大规模数据的软件平台.允许使用简单的编程模型在大量计算机集群上对大型数据集进行分 ...

  2. Python学习笔记:math模块(数学),random模块(随机数)

    math模块 math模块用于数学意义上的一些计算,常用的方法有: math.pi:PI的值(3.141592653589793). math.floor(x):返回一个小于等于x的最大整数(浮点类型 ...

  3. HihoCoder - 1636 Pangu and Stones(区间DP)

    有n堆石子,每次你可以把相邻的最少L堆,最多R堆合并成一堆. 问把所有石子合并成一堆石子的最少花费是多少. 如果不能合并,输出0. 石子合并的变种问题. 用dp[l][r][k]表示将 l 到 r 之 ...

  4. mysql不能登陆

    前些天还正常运行的mysql,不知怎么就不能登陆了.错误提示为 :ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (1 ...

  5. cacheData

    <%@ page language="java" import="java.util.*,com.fiberhome.bcs.appprocess.common.u ...

  6. logback mybatis 打印sql语句

    logbac.xml 文件的基础配置参考的园友的 http://www.cnblogs.com/yuanermen/archive/2012/02/13/2349609.html 然后hibernat ...

  7. 将FragmentManger事务添加到返回栈中

    FragmentManger事务添加或替换的 Fragment 后,这时点击 Back 键,程序并不会返回添加之前的状态. 我们可以使用 Transaction 对象的 addToBackStack( ...

  8. Codeforces 1139D(期望dp)

    题意是模拟一个循环,一开始有一个空序列,之后每次循环: 1.从1到m中随机选出一个数字添加进去,每个数字被选的概率相同. 2.检查这个序列的gcd是否为1,如果为1则停止,若否则重复1操作直至gcd为 ...

  9. 菜鸟之路——机器学习之SVM分类器学习理解以及Python实现

    SVM分类器里面的东西好多呀,碾压前两个.怪不得称之为深度学习出现之前表现最好的算法. 今天学到的也应该只是冰山一角,懂了SVM的一些原理.还得继续深入学习理解呢. 一些关键词: 超平面(hyper ...

  10. [oldboy-django][2深入django]分页功能

    1 django自带分页 1.1 分页模板 <!DOCTYPE html> <html lang="en"> <head> <meta c ...