[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 ...
随机推荐
- 一个用js写的接口http调试程序
公司有非常多手机app的项目.手机app又要常常訪问后台提交与查询数据. 所曾经端app与后台的开发与測试过程中接口调试是一个常常要做的工作. 而每当出现一个BUG,前端appproject师与后台p ...
- 十年linux命令总结
十年linux命令总结 本文链接: http://codingstandards.iteye.com/blog/786653 关于命令类型划分 本表中列出了我穷尽了我所有的记忆整理出来的Linux命令 ...
- 《循序渐进Oracle》部分笔记
1.不要用户名/密码 直接/as sysdba 是操作系统认证方式,改变安全方式 sqlnet.ora 里SQLNET.AUTHENTICATION_SERVICES=(NTS)表示启动操作系统认证; ...
- 集合中Set_List必须覆盖 hashCode()与 equals()
集合中Set_List必须覆盖 hashCode()与 equals() @Override public int hashCode() { System.out.println("==== ...
- Java学习之路:详细解释Java解析XML四种方法
XML如今已经成为一种通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便. 对于XML本身的语法知识与技术细节,须要阅读相关的技术文献,这里面包含的内容有DO ...
- Red Gate系列之一 SQL Compare 10.4.8.87 Edition 数据库比较工具 完全破解+使用教程
原文:Red Gate系列之一 SQL Compare 10.4.8.87 Edition 数据库比较工具 完全破解+使用教程 Red Gate系列之一 SQL Compare 10.4.8.87 E ...
- mysql安装前的系统准备工作(转)
一.系统环境总结:
- hadoop-ha组态
HADOOP HA组态 hadoop2.x的ha组态.这份文件是在那里的描述中hdfs与yarn的ha组态. 这份文件的假设是zk它已被安装并配置,事实上,任何安装. hdfs ha组态 首先.配置c ...
- Java 序列化Serializable详解(附详细例子)
Java 序列化Serializable详解(附详细例子) 1.什么是序列化和反序列化 Serialization(序列化)是一种将对象以一连串的字节描述的过程:反序列化deserialization ...
- 学习FFmpeg API – 解码视频
本文转载 视频播放过程 首先简单介绍以下视频文件的相关知识.我们平时看到的视频文件有许多格式,比如 avi, mkv, rmvb, mov, mp4等等,这些被称为容器(Container), 不同的 ...