【Combination Sum 】cpp
题目:
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的更多相关文章
- 【Path Sum】cpp
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- 【Two Sum】cpp
题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...
- 【Combination Sum II 】cpp
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- 【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 ...
- 【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 ...
- 【二叉树的递归】03判断二叉树中有没有和为给定值的路径【Path Sum】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树和一个和,判断这个树 ...
- 【Add binary】cpp
题目: Given two binary strings, return their sum (also a binary string). For example,a = "11" ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- 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~ ...
随机推荐
- 初学者:Git常用命令总结
git init 在本地新建一个repo,进入一个项目目录,执行git init,会初始化一个repo,并在当前文件夹下创建一个.git文件夹. git clone 获取一个u ...
- 用TextKit实现表情混排
Textkit是iOS7新推出的类库,其实是在之前推出的CoreText上的封装,有了这个TextKit,以后不用再拿着CoreText来做累活了,根据苹果的说法,他们开发了两年多才完成,而且他们 ...
- POJ 3187 Backward Digit Sums (递推,bruteforce)
第1行j列的一个1加到最后1行满足杨辉三角,可以先推出组合数来 然后next_permutation直接暴. #include<cstdio> #include<iostream&g ...
- hdu-1874 畅通工程续---模板题
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1874 题目大意: 求起点到终点的最短距离 解题思路: 注意重边,其他的就是模板 #include&l ...
- 简析平衡树(二)——Treap
前言 学完了替罪羊树,我决定再去学一学\(Treap\).一直听说\(Treap\)很难,我也花了挺久才学会. 简介 \(Treap\)这个名字真的挺有内涵: \(\color{red}{Tree}\ ...
- 2018.6.16 PHP小实验
PHP实验 实验一 <?php /** * Created by PhpStorm. * User: qichunlin * Date: 2018/5/17 * Time: 下午5:35 */ ...
- cd ..和cd -
cd ..是返回上一层目录, cd -是返回到上一次的工作目录.
- 如何更改VirtualBox虚拟电脑内存大小
- angular2的生命周期钩子的使用情况
angular 2 Directive Lifecycleangular2 中组建继承于指令,并扩展了与ui视图相关的属性.angular2 指令的生命周期是用来记录指令从创建,应用及销毁的过程.an ...
- phpstorm —— Xdebug 的配置和使用
给phpstorm 配置Xdebug(Xdebug 是 PHP 的一个扩展, 用于帮助调试和开发.它包含一个与 ide 一起使用的单步调试器.它升级了 PHP 的 var_dump () 功能) 这篇 ...