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, a1a2 ≤ … ≤ 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)的更多相关文章

  1. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  2. [LeetCode] Combination Sum III 组合之和之三

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. [LeetCode] Combination Sum II 组合之和之二

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  4. [LeetCode] Combination Sum 组合之和

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  5. LeetCode Combination Sum III

    原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...

  6. LeetCode: Combination Sum I && II && III

    Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...

  7. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  8. [Leetcode] Combination Sum 系列

    Combination Sum 系列题解 题目来源:https://leetcode.com/problems/combination-sum/description/ Description Giv ...

  9. LeetCode:Combination Sum I II

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

随机推荐

  1. BZOJ2190 [SDOI2008]仪仗队(欧拉函数)

    与HDU2841大同小异. 设左下角的点为(1,1),如果(1,1)->(x,y)和(1,1)->(x',y')向量平行,那只有在前面的能被看见.然后就是求x-1.y-1不互质的数对个数. ...

  2. 小明A+B[HDU2096]

    小明A+B Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  3. DWR入门教程

    DWR(Direct Web Remoting)是一个WEB远程调用框架.利用这个框架可以让AJAX开发变得很简单.利用DWR可以在客户端利用JavaScript直接调用服务端的Java方法并返回值给 ...

  4. OpenCV Open Camera 打开摄像头

    这是一个用OpenCV2.4.10打开摄像头的一个例子,参见代码如下: #include <iostream> #include <stdio.h> #include < ...

  5. hdu-acm steps Max sum

    /*求最大字段和,d[i]表示已 i 结尾(字段和中包含 i )在 a[1..i] 上的最大和,d[i]=(d[i-1]+a[i]>a[i])?d[i-1]+a[i]:a[i];max = {d ...

  6. PHP显示日期、周几、农历初几、什么节日函数编程代码

    <?function rili($style,$Atime){ $debug = false; $glmonth = date("n",$Atime);    //1-12  ...

  7. 增加Activity Monitor中的作业保存数量

    在Master Server的注册表中加入如下两个键值即可: (1500的单位是小时)  

  8. Xampp 添加 SSL

    我的 XAMPP 没有找到这句话 ,直接添加 extension=php_openssl.dll  大概988行另外,需要配置 httpd-ssl.conf 文件(*\xampp\apache\con ...

  9. JavaWEB前端向服务器端发送对象

    最近项目中需要做一个关于批量删除的功能,删除条件有多个,需要从页面全部传给后台服务器程序,单个的删除,可以拼接参数给url,服务器端获取参数后执行删除操作即可.但是批量删除多个,参数会很多,传递就有些 ...

  10. [办公自动化]利用Acrobat完成问卷调查或者考试卷

    整体思路:(软件环境Acrobat) 1.制作问卷. 采用word制作,制作基础页面,然后倒入.自己亲测时,发现一般的文字域是可以的,但是单选按钮就不能导入. 如果是考试卷,可以利用word制作基础页 ...