[leetcode] Combination Sum and Combination SumII
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 {
private:
vector<vector<int> > ivvec;
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
vector<int> ivec;
sort(candidates.begin(), candidates.end());
combinationSum(candidates, 0, target, ivec);
return ivvec;
}
void combinationSum(vector<int> &candidates, int beg, int target, vector<int> ivec)
{
if (0 == target)
{
ivvec.push_back(ivec);
return;
}
for (int idx = beg; idx < candidates.size(); ++idx)
{
if (target - candidates[idx] < 0)
break;
ivec.push_back(candidates[idx]);
combinationSum(candidates, idx, target - candidates[idx], ivec);
ivec.pop_back();
}
}
};
Given a collection of candidate numbers (C)
and a target number (T), find all unique combinations in C where
the candidate numbers sums to T.
Each number in C may only be used once in the combination.
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 10,1,2,7,6,1,5 and target 8,
A solution set is:
[1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]
与上题差别不大。依然是用DFS,基本的问题在于怎样去重。
能够添加一个剪枝: 当当前元素跟前一个元素是同样的时候。假设前一个元素被取了,那当前元素能够被取,也能够不取,反过来假设前一个元素没有取。那我们这个以及之后的所以同样元素都不能被取。
(採用flag的向量作为标记元素是否被选取)
class Solution {
private:
vector<vector<int> > ivvec;
vector<bool> flag;
public:
vector<vector<int> > combinationSum2(vector<int> &num, int target) {
vector<int> ivec;
sort(num.begin(), num.end());
flag.resize(num.size());
for (int i = 0; i < flag.size(); ++i)
flag[i] = false;
combinationSum2(num, 0, ivec, target, 0);
return ivvec;
}
void combinationSum2(vector<int> &num, int beg, vector<int> ivec, int target, int sum)
{
if (sum > target)
return;
if (sum == target)
{
ivvec.push_back(ivec);
return;
}
for (int idx = beg; idx < num.size(); ++idx)
{
if (sum + num[idx] > target) continue;
if (idx != 0 && num[idx] == num[idx - 1] && flag[idx - 1] == false)
continue;
flag[idx] = true;
ivec.push_back(num[idx]);
combinationSum2(num, idx + 1, ivec, target, sum + num[idx]);
ivec.pop_back();
flag[idx] = false;
}
}
};
版权声明:本文博主原创文章。博客,未经同意不得转载。
[leetcode] Combination Sum and Combination SumII的更多相关文章
- Combination Sum 和Combination Sum II
这两道题的基本思路和combination那一题是一致的,也是分治的方法. 其中combination Sum复杂一点,因为每个数可能用多次.仔细分析下,本质上也是一样的.原来是每个数仅两种可能.现在 ...
- Combination Sum,Combination Sum II,Combination Sum III
39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique co ...
- 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV
▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] Combination Sum 组合之和
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- [LeetCode] 377. Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] 40. Combination Sum II 组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
随机推荐
- SE 2014年5月25日
如图配置 两实验 R1模拟总部,R2 与R3模拟分部 实验一 要求使用 IPSec VPN 主模式,使得总部与两分部内网可相互通讯 步骤: 1. 配置默认路由 [RT1]ip route-stat ...
- 关于oracle的备份 导入
****假设要保存为bat文件.最好用汉字 导入: imp clsoftoa/clsoftoa@orcl124 --要导入的数据库的 username/password@数据库名 fromuser= ...
- quick-cocos2d-x游戏开发【4】——加入文本
文本的加入在quick中被封装在ui类中,它能够创建EditBox.菜单以及文本,文本总得来说能够创建TTF和BMFont两种. api对于它的说明非常具体.ui.newBMFontLabel(par ...
- cocos2D-x 3.5 引擎解析之--引用计数(Ref),自己主动释放池(PoolManager),自己主动释放池管理器( AutoreleasePool)
#include <CCRef.h> Ref is used for reference count manangement. If a classinherits from Ref. C ...
- MVC的DependencyResolver组件
MVC的DependencyResolver组件 一.前言 DependencyResolver是MVC中一个重要的组件,从名字可以看出,它负责依赖对象的解析,可以说它是MVC框架内部使用的一个IOC ...
- Android在 Alertdialog对话框中点击消失?
在开发的时候遇到一个问题.就是一触摸对话框边缘外部,对话框会自己主动消失.这个问题非常纠结啊,查找了一下发现从Android 4.0開始.AlertDialog有了变化.就是在触摸对话框边缘外部.对话 ...
- MVC模块化架构
全面解析ASP.NET MVC模块化架构方案 什么叫架构?揭开架构神秘的面纱,无非就是:分层+模块化.任意复杂的架构,你也会发现架构师也就做了这两件事. 本文将会全面的介绍我们团队在模块化设计方面取得 ...
- swift 学习资源 大集合
今天看到一个swift学习网站,其中我们收集了大量的学习资源 Swift 介绍 Swift 介绍 来自 Apple 官方 Swift 简单介绍 (@peng_gong) 一篇不错的中文简单介绍 [译] ...
- 二十7天 春雨滋润着无形 —Spring依赖注入
6月11日,明确."夏条绿已密,朱萼缀明鲜.炎炎日正午,灼灼火俱燃." IT人习惯把详细的事物加工成的形状一致的类.正是这种一致,加上合适的规范.才干彰显对象筋道的牙感和bean清 ...
- 基于j2ee的程序代写MVC架构
人力资源管理系统 完成系统静态页面设计,页面数量不少于10个,页面需用CSS进行美化,并为需要验证的信息利用JavaScript提供客户端验证.要求至少包含部门信息及部门内员工信息的添加.修改.删除和 ...