[LeetCode] Combination Sum (bfs)
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]
方法:用queue实现bfs
class Solution {
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
vector<vector<int> > result; if(candidates.size()==){
vector<int> tmp;
result.push_back(tmp);
return result;
}
sort(candidates.begin(),candidates.end()); bfs(result,candidates,target);
return result;
}//end func private:
void bfs(vector<vector<int> > &result,vector<int> &candidates,int target){
queue<vector<int> > q;
int len = candidates.size();
for(int i = ;i<len;i++){
int sum = ;
int value = candidates[i];
vector<int> tmp;
while(true){
sum += value;
tmp.push_back(value);
if(sum<target){
q.push(tmp);
}else if(sum == target){
if(find(result.begin(),result.end(),tmp)==result.end())
result.push_back(tmp);
}
else
break;
} while(!q.empty()){
tmp = q.front();
q.pop();
sum = ;
for(int k=;k<tmp.size();k++){
sum += tmp[k];
}
int sum0 = sum;
vector<int> tmp0(tmp);
for(int j=i+;j<len;j++){
value = candidates[j];
while(true){
sum += value;
tmp.push_back(value);
if(sum<target){
q.push(tmp);
}else if(sum == target){
sort(tmp.begin(),tmp.end());
if(find(result.begin(),result.end(),tmp)==result.end())
result.push_back(tmp);
}
else
break;
}
sum = sum0;
tmp = tmp0;
}
}
}//end for
}//end func
};
[LeetCode] Combination Sum (bfs)的更多相关文章
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [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 ...
- LeetCode Combination Sum III
原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...
- LeetCode: Combination Sum I && II && III
Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- [Leetcode] Combination Sum 系列
Combination Sum 系列题解 题目来源:https://leetcode.com/problems/combination-sum/description/ Description Giv ...
- LeetCode:Combination Sum I II
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
随机推荐
- 基于Extjs的web表单设计器 第五节——数据库设计
这里列出表单设计器系列的内容,6.7.8节的内容应该在春节后才有时间出了.因为这周末就请假回老家了,准备我的结婚大事.在此提前祝大家春节快乐! 基于Extjs的web表单设计器 基于Extjs的web ...
- ural 1150. Page Numbers
1150. Page Numbers Time limit: 1.0 secondMemory limit: 64 MB John Smith has decided to number the pa ...
- 移动APP 中文输入法下的搜索优化
最近做了一个移动端的搜索功能,带有suggest.实现上并没有什么可说的,但是在后续优化上,特别是在中文输入法的情况下的优化使我学到一些新东西,所以决定写一篇文章. 下面是我简化后的基本功能实现,监听 ...
- 转 Web移动应用调试工具——Weinre
如今人们也越来越习惯在手机上浏览网页,而在手机上这些针对桌面浏览器设计的网页经常惨不忍睹.Web应用开发者需要针对手机进行界面的重新设计,但是手机上并没有称心如意的调试工具(如Firebug.web ...
- js的url中传递中文参数乱码,如何获取url中参数问题
一:Js的Url中传递中文参数乱码问题,重点:encodeURI编码,decodeURI解码: 1.传参页面Javascript代码: <script type=”text/javascript ...
- CSV to XLSX (专用)
$csvFile = "F:\ACL\HZ ACL\ACL-APAC.CSV" $path = "F:\ACL\HZ ACL\ACL-APAC.XLSX" $r ...
- Scrum会议10(Beta版本) 补交
组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git.coding.n ...
- PHP $_SERVER 详解
元素/代码 描述 $_SERVER['PHP_SELF'] 当前执行脚本的文件名,与 document root 有关. $_SERVER['GATEWAY_INTERFACE'] 服务器使用的 CG ...
- [kuangbin带你飞]专题六 最小生成树
学习最小生成树已经有一段时间了 做一些比较简单的题还算得心应手..花了三天的时间做完了kuangbin的专题 写一个题解出来记录一下(虽然几乎都是模板题) 做完的感想:有很多地方都要注意 n == 1 ...
- 40. 特殊a串数列求和
特殊a串数列求和 #include <stdio.h> int main() { int i, a, n, item, sum, temp; while (scanf("%d % ...