【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~ ...
随机推荐
- 【iOS学习笔记】改变状态栏字体颜色
Step1. info.plist中设置UIViewControllerBasedStatusBarAppearance为NO Step2. AppDelegate.m中添加 - (BOOL)appl ...
- CSS变量(CSS variable)
使用 CSS 变量编写你的样式代码 基本使用: 1. --variable: <declaration-value> 2. <css-attribute>: var(--var ...
- 如何在Ubuntu server中修改IP
详细请移步至博客https://blog.csdn.net/shenzhen_zsw/article/details/74025066 方法一. sudo ifconfig eth0 100.100 ...
- Android商城开发系列(十)—— 首页活动广告布局实现
在上一篇博客当中,我们讲了频道布局的实现,接下来我们讲解一下活动广告布局的实现,效果如下图: 这个是用viewpager去实现的,新建一个act_item.xml,代码如下所示: <?xml v ...
- sublime完美编码主题
Theme – Soda 使用Ctrl+Shift+P快捷键或者进入菜单:Preferences(首选项) - Package Control(插件控制),调出命令输入框,输入Install Pack ...
- Redis多机数据库
复制 PSYNC命令具有完整重同步(full resynchronization)和部分重同步(partial resynchronization)两种模式: ·其中完整重同步用于处理初次复制情况:完 ...
- 一些好的IOS blog 不断增加中。。。。
http://www.swiftkiller.com/?p=371 http://blog.csdn.net/javayujiafeng/article/details/14163319 http:/ ...
- 【BZOJ1433】[ZJOI2009] 假期的宿舍(二分图匹配入门)
点此看题面 大致题意:有\(n\)个学生,其中一部分是在校学生,一部分不是,而在校学生中一部分回家,一部分不回家,并且我们用一个01矩阵表示学生之间相互认识关系.已知每个学生只能睡自己认识的人的床(当 ...
- 在TreeView控件节点中显示图片
实现效果: 知识运用: TreeView控件中Nodes集合的Add方法 //创建节点并将节点放入集合中 public virtual TreeNode Add (string key,string ...
- cuda科普像素坐标和线性偏移
cuda科普像素坐标和线性偏移