Combination Sum [LeetCode]
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]的更多相关文章
- 39. Combination Sum - LeetCode
Question 39. Combination Sum Solution 分析:以candidates = [2,3,5], target=8来分析这个问题的实现,反向思考,用target 8减2, ...
- Combination Sum —— LeetCode
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- Combination Sum leetcode java
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] Backtracking Template for (Subsets, Permutations, and Combination Sum)
根据issac3 用Java总结了backtracking template, 我用他的方法改成了Python. 以下为template. def backtrack(ans, temp, nums, ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] Combination Sum 组合之和
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- 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 ...
随机推荐
- missing sdkl in .NET Core 1.0.1 - VS 2015 Tooling Preview 2
打开项目的时候,提示缺少sdk 在C:\Program Files\dotnet\sdk找不到对应的版本 解决方法: https://github.com/aspnet/Tooling/blob/ma ...
- linux中mysql基本操作
1.linux下启动mysql的命令: mysqladmin start /ect/init.d/mysql start (前面为mysql的安装路径) 2.linux下重启mysql的命令: mys ...
- 使用source Insight工具创建uboot工程。
首先在linux下面解压uboot的代码.不能在Windows下面解压,因为Windows的文件名是不区分大小写的. 然后,创建网络驱动器,这样就能在Windows下访问linux的文件夹了.方法:通 ...
- [Effective Java]第七章 方法
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- 加载xib文件
// Test.xib --编译--> Test.nib // 方式1 NSArray *objs = [[NSBundle mainBundle] loadNibNamed:@"Te ...
- FZU 2212 Super Mobile Charger(超级充电宝)
[Description] [题目描述] While HIT ACM Group finished their contest in Shanghai and is heading back Harb ...
- SQL collate
摘自:http://www.cnblogs.com/window5549-accp/archive/2009/10/03/1577682.html 我们在create table时经常会碰到这样的语句 ...
- MongoDB开发学习
如果你从来没有接触MongoDB或对MongoDB有一点了解,如果你是C#开发人员,那么你不妨花几分钟看看本文.本文将一步一步带您轻松入门. 阅读目录 一:简介 二:特点 三:下载安装和开启服务器 四 ...
- maven2 配置M2_REPO
从eclipse中增加了maven2的插件之后,maven默认的本地库的路径是${user}/.m2/repository/下,一般windows用户的操作系统都安装在C盘,所以这个目录 下的jar包 ...
- intanceof以及引出的__proto__和prototype
instanceof运算代码 function instance_of(L, R) { //L 表示左表达式,R 表示右表达式 var O = R.prototype; // 取 R 的显示原型 L ...