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.

 class Solution {
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
// Note: The Solution object is instantiated only once and is reused by each test case.
std::sort(candidates.begin(), candidates.end());
vector<vector<int> > combinations;
for(int i = ; i < candidates.size(); i ++) {
if(candidates[i] > target)
break;
if(candidates[i] == target) {
vector<int> combination;
combination.push_back(candidates[i]);
combinations.push_back(combination);
}else{
if(i == && target % candidates[i] != )
continue;
if(i == && target % candidates[i] == ){
vector<int> combination(target / candidates[i], candidates[i]);
combinations.push_back(combination);
continue;
}
vector<int> sub_candidates(candidates.begin(), candidates.begin() + i + );
vector<vector<int> > rests = combinationSum(sub_candidates, target - candidates[i]);
for(auto item: rests){
item.push_back(candidates[i]);
combinations.push_back(item);
}
}
}
return combinations;
}
};

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. 从原理上搞定编码-- Base64编码

    BASE64是一种编码方式,通常用于把二进制数据编码为可写的字符形式的数据.这是一种可逆的编码方式.编码后的数据是一个字符串,其中包含的字符为:A-Z.a-z.0-9.+./共64个字符:26 + 2 ...

  2. DevOps到底是什么?

    本篇将讨论DevOps到底包含什么,今后的运维工程师应该朝什么方向努力.

  3. R语言画图基础参数设置

    Graphical Parameters You can customize many features of your graphs (fonts, colors, axes, titles) th ...

  4. ORA-01009: 必需的参数缺失

    第一步:看看是否有参数没有配: 第二步:如果第一步没问题,那么请在英文半角下把sql语句重新写一遍 以上~

  5. ie8不支持transform: translateY,ie9支持不友好

    transform: translateY(0);  ie8 不支持这个属性  但是我觉得用css实现的效果特好 特自然 也许是我的脚本写的不够完善 呵呵    现在我希望其他 主流的浏览器应用CSS ...

  6. SQL server数据类型、增删改查

    数据类型: 整数型:bigint.int.smallint.mediumint.tinyint 小数类型:decimal.numeric 浮点型:real.float.double 位型:bit 字符 ...

  7. (linux) 常用命令 干货

    内容提要 1. 第一难度, 重要 梯队 (grep, awk, sed, find, ) 2. 第二常用, 重要 梯队 (ps, ls, date, fuser, netstat, kill) 3. ...

  8. 三种实例化bean的方式

    在spring中有三中实例化bean的方式: 一.使用构造器实例化:(90%通常使用的一个方法) 二.使用静态工厂方法实例化: 三.使用实例化工厂方法实例化. 每种实例化所采用的配置是不一样的: 一. ...

  9. UIButton(在代码中使用)

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

  10. SurfaceHolder.Callback

    Class Overview A client may implement this interface to receive information about changes to the sur ...