[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 ...
随机推荐
- 如何:从代码创建 UML 类图(ZZ)
您拖动的一个或多个类将显示在关系图上. 它们依赖的类将显示在"UML 模型资源管理器"中. 参见 模型表示类型的方式. 将程序代码中的类添加到 UML 模型 打开一个 C# 项 ...
- [Cocos2d-x For WP8]Particle粒子系统
在游戏中,经常要实现一些真实的效果,这些效果(如,火焰,雪花等)都是由大量微粒组合而形成的.为了在游戏中实现这种效果,我们必须引进粒子系统,粒子系统中需要包括四个部分:粒子对象,运动规律,随机性,粒子 ...
- UOJ#61. 【UR #5】怎样更有力气
大力水手问禅师:“大师,很多事情都需要用很大力气才能完成,而我在吃了菠菜之后力气很大,于是就导致我现在非常依赖菠菜.我很讨厌我的现状,有没有办法少吃点菠菜甚至不吃菠菜却仍很有力气?” 禅师浅笑,答:“ ...
- try {}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,还是在return之后执行?
这是一个很有趣的问题,我测试的结果是:是在return中间执行. 我在网上搜寻了一些资料,下面是参考代码: /** * */ package com.b510.test; /** * try {}里有 ...
- 1.Java为什么能跨平台运行?请简述原理。
使用不同操作系统的JVM(JAVA虚拟机)解释运行编译好的字节码文件(.class)
- Twemproxy 介绍与使用
Twemproxy是一种代理分片机制,由Twitter开源.Twemproxy作为代理,可接受来自多个程序的访问,按照路由规则,转发给后台的各个Redis服务器,再原路返回.该方案很好的解决了单个Re ...
- 出现segment fault 错误的几种原因
segment fault 段异常各种原因www.MyException.Cn 发布于:2012-11-26 11:48:50 浏览:24次 0 segment fault 段错误各种原因一 造成se ...
- ASP.NET WEB API的服务托管(Self-HOST)
如果我们想对外发布RESTful API,可以基于ASP.NET来构建Restful APIs,但需要部署IIS吗?答案是不必.你可以把它托管到一个Windows Service.具体如何把WEB A ...
- 【液晶模块系列基础视频】1.2.iM_RGB模块介绍
[液晶模块系列基础视频]1.2.iM_RGB模块介绍(上) [液晶模块系列基础视频]1.2.iM_RGB模块介绍(下) ============================== 技术论坛:http ...
- Grand Theft Auto V 图形研究(3)
原文链接 http://www.adriancourreges.com/blog/2015/11/02/gta-v-graphics-study-part-3/ Post Processing Eff ...