题目:

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. Citrix-Netscaler-VPX-11.0

    平台: CentOS 类型: 虚拟机镜像 软件包: Citrix linux basic software Loadbalance security waf wlb 服务优惠价: 按服务商许可协议 云 ...

  2. 轻松搞定Struts 2:三步走上手小入门

    零.Struts 2是啥? SSH.SSM.SSI如雷贯耳,Struts 2 —— 是的,就这样了... 一.Hello Struts2 1.核心包 2.struts.xml核心控制器配置 <f ...

  3. 地理位置索引 2d索引

    地址位置索引:将一些点的位置存储在mongodb中,创建索引后,可以按照位置来查找其他点 子分类: .2d索引:平面地理位置索引,用于存储和查找平面上的点. .2dsphere索引:球面地理位置索引, ...

  4. Matlab 中实用数据结构之 containers.Map

    概要   熟悉 Python 的都知道字典 Dict 类型数据结构功能的强大,Matlab 中虽然有表结构,但是其列名必须是亦变量名类型的字符串,如果我想用数字开头的字符串作键值,其表结构就无能为力了 ...

  5. https及其背后的加密原理阅读总结

    https是以安全为目标的http通道,简单讲是http的安全版.当我们往服务器发送比较隐私的数据(比如说你的银行卡,身份证)时,如果使用http进行通信.那么安全性将得不到保障. 首先数据在传输的过 ...

  6. 使用Vue CLI 3快速创建项目

    首先 vue create ant-design-vue-pro 执行命令会显示两个选项,1默认,2自定义 我么选择自定义 选择好自定义的插件回车就等待下安装成功然后进入项目文件夹 cd ant-de ...

  7. IATHook

    IATHookClass.h #pragma once #include <Windows.h> class IATHookClass { private: DWORD oldAddr; ...

  8. (77)zabbix主动、被动检测的详细过程与区别

    zabbix agent检测分为主动(agent active)和被动(agent)两种形式,主动与被动的说法均是相对于agent来讨论的.简单说明一下主动与被动的区别如下: 主动:agent请求se ...

  9. centos7安装mongodb3.6

    1. 安装一下centos(6.5) + 虚拟机,在VMware中安装mongodb 2. 下载mongodb最新版本:mongodb-linux-x86_64-3.6.4.tgz,传到centos ...

  10. PHP代码中出现中文乱码怎么办?

    header("Content-type:text/html;charset=utf-8"); //加上这个就OK //示例 : class WechatController ex ...