Leetcode-39.Combination Sum
2015-12-27 04:35
274 查看
Problem description :
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
Note:
All numbers (including target) will be positive integers.
Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
The solution set must not contain duplicate combinations.
For example, given candidate set 2,3,6,7 and target 7,
A solution set is:
[7]
[2, 2, 3]
Analysis :
1. What is a partial solution and when is it finished?
Partial Solution is the combination, finished when sum of the set is equal to target.
2. How to find all the partial solutions?
Using a for-loop to traverse all the possible elements in candidates.
3. When to make recursive calls?
when add a new element to the partial solution, we use recursive call to generate all the results.
Remember to recover to the previous status once a partial solution is done,
Code:
We also have dynamic programming solution:
This is just like a coin change/knapsack problem,.We need to create a vector of the size of target. And for each o(1)…o(target) we get the values. And if in between we find any sum == target, add that into the result
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
Note:
All numbers (including target) will be positive integers.
Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
The solution set must not contain duplicate combinations.
For example, given candidate set 2,3,6,7 and target 7,
A solution set is:
[7]
[2, 2, 3]
Analysis :
1. What is a partial solution and when is it finished?
Partial Solution is the combination, finished when sum of the set is equal to target.
2. How to find all the partial solutions?
Using a for-loop to traverse all the possible elements in candidates.
3. When to make recursive calls?
when add a new element to the partial solution, we use recursive call to generate all the results.
Remember to recover to the previous status once a partial solution is done,
Code:
class Solution { public: vector<vector<int> > combinationSum(vector<int>& candidates, int target) { int n = candidates.size(); vector<int> v; vector<vector<int> > res; if (n == 0) return res; sort(candidates.begin(), candidates.end()); combine(res, v, candidates, target, 0); return res; } void combine(vector<vector<int> >& res, vector<int>& v, vector<int>& candidates, int target, int start) { if (target == 0) { res.push_back(v); return ; } for (int i = start; i < candidates.size() && candidates[i] <= target; ++i)// <= { v.push_back(candidates[i]); combine(res, v, candidates, target - candidates[i], i); v.pop_back(); } } };
We also have dynamic programming solution:
This is just like a coin change/knapsack problem,.We need to create a vector of the size of target. And for each o(1)…o(target) we get the values. And if in between we find any sum == target, add that into the result
class Solution { public: vector<vector<int> > combinationSum(vector<int>& candidates, int target) { sort(candidates.begin(),candidates.end()); vector<vector<int> > res; vector<vector<vector<int> > > dp(target + 1, vector<vector<int>>()); dp[0].push_back(vector<int>()); for (auto& num : candidates) { for (int j = 0; j <= target - num; ++j) { auto s = dp[j]; if (s.size() > 0) { for (auto& t : s) t.push_back(num); dp[j + num].insert(dp[j + num].end(), s.begin(), s.end()); } } } return dp[target]; } };
相关文章推荐
- iOS objc在simulator-x86_64体系下一般传参方式
- 拦截器示例之日志记录
- Maven with Scala
- scala + intellij idea 环境搭建及编译、打包
- nginx 安装手记
- HDU 2082 找单词 --- 母函数
- 如何正确看待手机的续航
- 陈坤:一个贫穷而美貌的男人在这世上可能遭遇什么
- Scrum Meeting 14-20151227
- centos7图形配置 firewall-config
- centos7图形配置 firewall-config
- CLR via C#深解笔记三 - 基元类型、引用类型和值类型 | 类型和成员基础 | 常量和字段
- 网页全栈工程师要点分析
- java使用jUnit,jMockit,dbunit做DAO层单元测试方法
- 拦截器与过滤器的区别
- Junit测试编程
- 在Activity之间使用Intent传值和Bundle传值的区别和方式
- 连京东都开始卖翻新机,教你识别手中的iPhone 隐藏id
- notepad++实现多处编辑
- cas client入门:web.xml 配置