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. BZOJ3828 : [Poi2014]Criminals

    对于每个位置求出L[i]表示左边最大的j,满足从j开始到i-1中存在第一个子序列 R[i]表示右边最小的j,满足从j开始到i-1中存在第二个子序列 然后枚举颜色是相遇点的位置,如果L[i]左边.R[i ...

  2. Android开源框架:AndroidAnnotations

    AndroidAnnotations首页 github上的项目地址AndroidAnnotations Github. wiki:https://github.com/excilys/androida ...

  3. 【wikioi】1049 棋盘染色(迭代深搜)

    http://www.wikioi.com/problem/1049/ 这题我之前写没想到迭代加深,看了题解,然后学习了这种搜索(之前我写的某题也用过,,但是不懂专业名词 囧.) 迭代加深搜索就是限制 ...

  4. pygame系列_百度随心听_完美的UI设计

    这个程序的灵感来自于百度随心听 下面是我的程序截图: 说明: 动作按钮全部是画出来的,没有用到任何图片 用到图片的只有:背景,歌手图片,作者图片 代码正在调试中.... 如果你鼠标移动到黄色小圆里面, ...

  5. RowDataBound事件

    RowDataBound事件在创建gridView控件时,必须先为GridView的每一行创建一个GridViewRow对象,创建每一行时,将引发一个RowCreated事件:当行创建完毕,每一行Gr ...

  6. 【C语言】15-预处理指令1-宏定义

    预处理指令简介 1.C语言在对源程序进行编译之前,会先对一些特殊的预处理指令作解释(比如之前使用的#include文件包含指令),产生一个新的源程序(这个过程称为编译预处理),之后再进行通常的编译 2 ...

  7. oracle在线重定义表

    在一个高可用系统中,如果需要改变一个表的定义是一件比较棘手的问题,尤其是对于7×24系统.Oracle提供的基本语法基本可以满足一般性修改,但是对于把普通堆表改为分区表,把索引组织表修改为堆表等操作就 ...

  8. 对thinkphp静态模板表单提交的理解

    看表单的提交<form action="{$Think.const.__SELF__}"  method="post">...</form&g ...

  9. Linux启动过程中几个重要配置文件的执行过程

    Linux 登录后,配置执行顺序为(Debian Serials Capable):/etc/environment -> /etc/profile -> (~/.bash_profile ...

  10. 算法与数据结构题目的 PHP 实现:栈和队列 设计一个有 getMin 功能的栈

    刚入手了一本<程序员代码面试指南>,书中题目的代码都是 Java 实现的,琢磨着把这些代码用 PHP 敲一遍,加深印象. 题目:设计一个有 getMin 功能的栈 —— 实现一个特殊的栈, ...