题目:

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]

代码:

class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int> > ret;
vector<int> tmp;
int sum = ;
std::sort(candidates.begin(), candidates.end());
Solution::dfs(ret, tmp, sum, candidates, , candidates.size()-, target);
return ret;
}
static void dfs(
vector<vector<int> >& ret,
vector<int>& tmp,
int &sum,
vector<int>& candidates,
int begin,
int end,
int target )
{
if ( sum>target ) return;
if ( sum==target )
{
ret.push_back(tmp);
return;
}
for ( int i=begin; i<=end; ++i )
{
if ( sum+candidates[i]<=target )
{
sum += candidates[i];
tmp.push_back(candidates[i]);
Solution::dfs(ret, tmp, sum, candidates, i, end, target);
sum -= candidates[i];
tmp.pop_back();
}
}
}
};

tips:

采用深搜模板:

1. 终止条件sum>target

2. 加入解集条件sum==target

3. 遍历当前层所有分支(如果满足sum+candidates[i]<target,则还可以再往上加candidates[i];注意,这里传入下一层的begin下标为i,因为要求元素可以无限多重复)

4. 由于传入下一层始终满足begin<=end,因此不要在终止条件中加入(begin>end)

=======================================

第二次过这道题,用dfs的思路,一次AC了。

class Solution {
public:
vector<vector<int> > combinationSum(
vector<int>& candidates,
int target)
{
vector<vector<int> > ret;
vector<int> tmp;
sort(candidates.begin(), candidates.end());
Solution::dfs(ret, tmp, candidates, , candidates.size()-, target);
return ret;
}
static void dfs(
vector<vector<int> >& ret,
vector<int>& tmp,
vector<int>& candidates,
int begin,
int end,
int target
)
{
if ( target< ) return;
if ( target== )
{
ret.push_back(tmp);
return;
}
for ( int i=begin; i<=end; ++i )
{
tmp.push_back(candidates[i]);
Solution::dfs(ret, tmp, candidates, i, end, target-candidates[i]);
tmp.pop_back();
}
}
};

【Combination Sum 】cpp的更多相关文章

  1. 【Path Sum】cpp

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  2. 【Two Sum】cpp

    题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...

  3. 【Combination Sum II 】cpp

    题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...

  4. 【Binary Tree Maximum Path Sum】cpp

    题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...

  5. 【Minimum Path Sum】cpp

    题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...

  6. 【二叉树的递归】03判断二叉树中有没有和为给定值的路径【Path Sum】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树和一个和,判断这个树 ...

  7. 【Add binary】cpp

    题目: Given two binary strings, return their sum (also a binary string). For example,a = "11" ...

  8. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  9. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

随机推荐

  1. Spring+Hibernateh使用小结

    由此我们可以看出,报出错误的地方主要是slf4j的jar包,而故障码中“Failed to load class ’org.slf4j.impl.StaticLoggerBinder‘”的意思则是“加 ...

  2. SpringBoot的核心功能

    1.独立运行的Spring项目 SpringBoot可以以jar包的形式独立运行,运行一个SpringBoot项目只需要通过java -jar xx.jar来启动. 2.内嵌Servlet容器 Spr ...

  3. C#启动或停止 计算机中“服务”

    第一.要添加一个引用System.ServiceProcess 第二.要在程序中使用命名空间ServiceProcess 代码片段: using System.ServiceProcess; Serv ...

  4. 小div在大div里面 垂直居中

    方法1: .parent { width:800px; height:500px; border:2px solid #000; position:relative; } .child { width ...

  5. 数据字典的设计--3.首页添加删除表格(JS实现)

    页面效果: JS代码: 1.添加表格 function insertRows(){ //获取表格对象 var tb1 = $("#dictTbl"); var tempRow = ...

  6. v-if与v-show的区别

    一.区别 v-if 动态的向DOM树内添加或者删除DOM元素:“真正”的条件渲染,因为它会确保在切换过程中条件块内的事件监听器和子组件适当地被销毁和重建:在初始渲染条件为假时,什么也不做. v-sho ...

  7. JavaScript-判断语句(if...else)

    语法: if(条件) { 条件成立时执行的代码 } else { 条件不成立时执行的代码 } 假设我们通过年龄来判断是否为成年人,如年龄大于等于18岁,是成年人,否则不是成年人.代码表示如下: < ...

  8. TP5.0:的安装与配置

    在网址中输入:localhost/安装TP5的文件夹/public/ 入口文件位置:public/index.php: 最新版本中,新建的文件夹是没有模型和视图的,需要自行添加没有的文件: 添加前: ...

  9. nginx搭建流媒体服务器

    1.安装PCRE库 到www.pcre.org 下载pcre-8.37.tar.gz tar -zxvf pcre-8.37.tar.gz cd pcre-8.37 ./configure make ...

  10. IOS 强指针(strong)和弱指针(weak)

    // strong 强指针        // weak 弱指针        // ARC, 只要对象没有强指针就会自动释放        // OC中默认都是强指针