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. SIP进行时

    一直以“简单”著称的SIP其实也没那么简单,不过任何事物想掌握它都很困难. 这篇文档旨在不断的记录SIP使用过程中遇到的各种疑惑和问题. 一.响应422 Session Interval Too Sm ...

  2. [HDOJ1175]连连看

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175 连连看 Time Limit: 20000/10000 MS (Java/Others)     ...

  3. [SAP ABAP开发技术总结]面向对象OO

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  4. JavaScript经典代码【一】【javascript HTML控件获取值】

    javascript HTML控件获取值 1.下拉列表框选定值 ddlPageSize.options[ddlPageSize.selectedIndex].value ddlPageSize.opt ...

  5. 【网络流24题----03】Air Raid最小路径覆盖

    Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  6. CSS笔记(七)列表

    CSS 列表属性允许你放置.改变列表项标志,或者将图像作为列表项标志. 参考:http://www.w3school.com.cn/css/css_list.asp 实例: <html> ...

  7. iOS - Swift Foundation 框架

    前言 框架是由许多类.方法.函数和文档按照一定的逻辑组织起来的集合,以使研发程序更容易. Foundation 框架:为所有程序开发奠定基础的框架称为 Foundation 框架. Cocoa :是指 ...

  8. Redis基础知识之————php-Redis 常用命令专题

    Keys del,delete - 删除键 dump - 返回存储在指定键值的序列化版本. exists - 确定键是否存在 expire,setTimeout,pexpire - 设置键的生存时间( ...

  9. jqurey click和blur执行时间冲突

    参考资料:http://stackoverflow.com/questions/10652852/jquery-fire-click-before-blur-event

  10. 国内外常用的DNS服务器

    国内外常用的DNS服务器 DNS,全称Domain Name System,即域名解析系统,帮助用户在互联网上寻找路径,它在互联网的作用是把域名转换成为网络可以识别的IP地址. 国外DNS服务器地址: ...