[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 ...
随机推荐
- Codeforces 86D Powerful array(莫队算法)
和BZOJ2038差不多..复习一下. #include<cstdio> #include<cmath> #include<algorithm> using nam ...
- [转]C++设计模式:Builder模式
Builder模式要解决的问题是,当我们要创建很复杂的对象时,有时候需要将复杂对象的创建过程和这个对象的表示分离开来.由于在每一步的构造过程中可以映入不同参数,所以步骤相同但是最后的对象却不一样.也就 ...
- object-c [self class] 和 [self _cmd]
[self class] 返回当前类名 [self _cmd] 返回当前方法名 self 是类的隐藏的参数,指向当前当前调用方法的类 另一个隐藏参数是_cmd,代表当前类方法的selector
- 【BZOJ】1040: [ZJOI2008]骑士(环套树dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1040 简直不能再神的题orz. 蒟蒻即使蒟蒻,完全不会. 一开始看到数据n<=1000000就 ...
- 全面解析Linux数字文件权限
全面解析Linux数字文件权限 来源: 时间:2013-09-04 20:35:13 阅读数:11433 分享到:0 [导读] 在刚开始接触Linux时对于文件权限的理解并不是很透彻,这里详细 ...
- overload和override的区别(转)
overload和override的区别 override(重写) 1.方法名.参数.返回值相同.2.子类方法不能缩小父类方法的访问权限.3.子类方法不能抛出比父类方法更多的异常(但子类方法可以不抛出 ...
- Python 处理理时间超详细转的
# -*- coding: utf-8 -*- import time def timestamp_datetime(value): format = '%Y-%m-%d %H:%M:%S' ...
- iOS - AVAudioPlayer 音频播放
前言 NS_CLASS_AVAILABLE(10_7, 2_2) @interface AVAudioPlayer : NSObject @available(iOS 2.2, *) public c ...
- OpenCV学习笔记——滑动条开关
由于opencv库中并没有专门为开关而设的函数,可以用滑动条做开关 代码: #include<highgui.h> #include<cv.h> int g_switch_va ...
- 从StackOverflow来的值得回味的编程观点
从StackOverflow来的值得回味的编程观点 很多有意思的话语 在 2012年06月08日 那天写的 已经有 4148 次阅读了 感谢 参考或原文 www.csdn.net 服务器君 ...