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. 机器学习——SVM详解(标准形式,对偶形式,Kernel及Soft Margin)

    (写在前面:机器学习入行快2年了,多多少少用过一些算法,但由于敲公式太过浪费时间,所以一直搁置了开一个机器学习系列的博客.但是现在毕竟是电子化的时代,也不可能每时每刻都带着自己的记事本.如果可以掏出手 ...

  2. Beaglebone Black - 控制 BBB 板上的 LED 灯

    BBB 的板上有五个 LED 灯,一个电源,四个其他指示灯,usr0 至 usr3 .这次学习是控制 usr0 至 3 让它们亮着,熄灭,闪.算是个 Hello World 实验.非常简单. 需要的材 ...

  3. arp中间人

    0x00 摘要 在本章第二层攻击当中,我们将进入网络hacking的奇幻之旅.让我们回顾一下,第二层是负责在以太网中,使用MAC地址来发送数据包.除了ARP攻击,我们将探讨交换机是如何应对DOS攻击的 ...

  4. CUBRID学习笔记 37 ADO.NET Schema Provider

    通常需要添加以下引用:   1 2 3 using System.Data; using System.Data.Common; using CUBRID.Data.CUBRIDClient; 定义连 ...

  5. linux的计划任务crontab

    crontab(全称cron table计划任务列表)是一个用于周期性被执行的任的工具. 相关指令: usage: crontab [-u user] file   crontab [ -u user ...

  6. Linux基础※※※※访问Windows共享文件夹

    参考Linux公社链接:http://www.linuxidc.com/Linux/2014-06/103749.htm 实际上,可以直接用sambaclient程序访问共享资源. 列出共享主机的列表 ...

  7. SAP 批量查看凭证更改记录

    1,在凭证上点击环境->凭证变更 查找.2,通过运行程序 SE38:RSSCD1TS 根据对象类.对象标识查找. 3,SE16N/SE16/SE11查看标准表,CDHDR(更改凭证抬头),CDP ...

  8. 刻录DVD_目录

    1.down.52pojie.cn (20160701) (1 in 5) 工具 2.down.52pojie.cn (20160701) (2 in 5) Android VM 6/7 3.down ...

  9. mysql 存储过程中limit

    1.mysql的高版本(5.5),存储过程中的limit可以使用变量,如下:select * from student limit iStart,iNum; 2.mysql的低版本(5.1),存储过程 ...

  10. 2015苹果WWDC开发者大会

    2015苹果WWDC开发者大会 (1)本届主题为“the epicenter of change(变革的中心)” (2)iOS 9.OS X.watchOS三款重要系统更新以及其他服务 (3)iOS ...