Problem Description: http://oj.leetcode.com/problems/combination-sum/    

Basic idea: It seems complicate at first. But once I got the recursive idea, the solution was neat. This code could be not efficient. I will rewrite with dynamic programming next time.

  1. class Solution {
  2. public:
  3. vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
  4. // Note: The Solution object is instantiated only once and is reused by each test case.
  5. std::sort(candidates.begin(), candidates.end());
  6. vector<vector<int> > combinations;
  7. for(int i = ; i < candidates.size(); i ++) {
  8. if(candidates[i] > target)
  9. break;
  10. if(candidates[i] == target) {
  11. vector<int> combination;
  12. combination.push_back(candidates[i]);
  13. combinations.push_back(combination);
  14. }else{
  15. if(i == && target % candidates[i] != )
  16. continue;
  17. if(i == && target % candidates[i] == ){
  18. vector<int> combination(target / candidates[i], candidates[i]);
  19. combinations.push_back(combination);
  20. continue;
  21. }
  22. vector<int> sub_candidates(candidates.begin(), candidates.begin() + i + );
  23. vector<vector<int> > rests = combinationSum(sub_candidates, target - candidates[i]);
  24. for(auto item: rests){
  25. item.push_back(candidates[i]);
  26. combinations.push_back(item);
  27. }
  28. }
  29. }
  30. return combinations;
  31. }
  32. };

Combination Sum [LeetCode]的更多相关文章

  1. 39. Combination Sum - LeetCode

    Question 39. Combination Sum Solution 分析:以candidates = [2,3,5], target=8来分析这个问题的实现,反向思考,用target 8减2, ...

  2. Combination Sum —— LeetCode

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

  3. Combination Sum leetcode java

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

  4. [LeetCode] Combination Sum III 组合之和之三

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

  5. [LeetCode] Backtracking Template for (Subsets, Permutations, and Combination Sum)

    根据issac3 用Java总结了backtracking template, 我用他的方法改成了Python. 以下为template. def backtrack(ans, temp, nums, ...

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

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

  7. [LeetCode] Combination Sum II 组合之和之二

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

  8. [LeetCode] Combination Sum 组合之和

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

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

随机推荐

  1. 【转载】.NET面试题系列[0] - 写在前面

    原文:.NET面试题系列[0] - 写在前面 索引: .NET框架基础知识[1] - .NET框架基础知识(1) http://www.cnblogs.com/haoyifei/p/5643689.h ...

  2. Creating, Stopping, Re-Starting and Deleting a Timer in Oracle Forms

    I have written many posts previously on Timers in Oracle Forms like how to change images randomly wi ...

  3. MYSQL写入数据时报错ERROR 1366 (HY000): Incorrect string value: '\xE8\x8B\xB1\xE5\xAF\xB8...' for c 插入中文不能插入

    先把原先你创建的这个表删除,然后 CREATE TABLE IF NOT EXISTS tdb_goods( goods_id SMALLINT UNSIGNED PRIMARY KEY AUTO_I ...

  4. [转]-用Gradle 构建你的android程序

    出处:http://www.cnblogs.com/youxilua  前言 android gradle 的插件终于把混淆代码的task集成进去了,加上最近,android studio 用的是gr ...

  5. 个人阅读作业 The Last

    对于软件工程M1/M2的总结: 假象-MO 在团队开发的前期,我感觉自己其实给了自己很多的期待,因为一直希望着自己可以在团队中担任一个角色,用自己的力量为团队多做事情,也给了其他人一些假象,那就是看起 ...

  6. Nginx模块学习之————accesskey权限模块使用(简单的m3u8防盗链)

    配置文件:http://www.cnblogs.com/tinywan/p/5983694.html 通过加密后的文件: 正确地址:curl -i http://访问的IP地址(这里是直播节点IP地址 ...

  7. Redis基础知识之————使用技巧(持续更新中.....)

    一.key 设计技巧 把表名转换为key前缀 如, tag: 第2段放置用于区分区key的字段--对应mysql中的主键的列名,如userid 第3段放置主键值,如2,3,4...., a , b , ...

  8. DZY Loves Chessboard

    DescriptionDZY loves chessboard, and he enjoys playing with it. He has a chessboard of n rows and m ...

  9. (二)程序中的内存&&栈

    一.程序运行为什么需要内存?基本概念? 内存是程序运行的立足之地,程序需要用内存来存储一些变量. 内存管理最终是由操作系统完成的,内存在本质上是一个硬件器件,由硬件系统提供:内存由操作系统统一管理,为 ...

  10. UIButton(在代码中使用)

    - (void)viewDidLoad { [super viewDidLoad]; // 1.1 创建按钮对象 // UIButton *button = [[UIButton alloc] ini ...