[Leetcode] Combination Sum 系列
Combination Sum 系列题解
题目来源:https://leetcode.com/problems/combination-sum/description/
Description
Given a set of candidate numbers (C) (without duplicates) 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.
- The solution set must not contain duplicate combinations.
Example
For example, given candidate set [2, 3, 6, 7]
and target 7
,
A solution set is:
[
[7],
[2, 2, 3]
]
Solution
class Solution {
private:
void backTrack(vector<int>& path, vector<vector<int> >& res,
vector<int>& candidates, int begin, int target) {
if (target == 0) {
res.push_back(path);
} else {
int size = candidates.size();
if (begin >= size)
return;
for (int i = begin; i < size; i++) {
if (candidates[i] <= target) {
path.push_back(candidates[i]);
backTrack(path, res, candidates, i, target - candidates[i]);
path.pop_back();
}
}
}
}
public:
vector<vector<int> > combinationSum(vector<int>& candidates, int target) {
vector<vector<int> > res;
vector<int> path;
backTrack(path, res, candidates, 0, target);
return res;
}
};
解题描述
这道题类似与经典的零钱兑换问题,在给定的数组candidates
,找出所有和为target
的数字组合,选择的数字可以重复但解法不能重复。上面使用的是递归回溯的办法,类似DFS,不难理解,下面再多给出使用迭代DP的解法:
class Solution {
public:
vector<vector<int> > combinationSum(vector<int>& candidates, int target) {
vector<vector<vector<int> > > dp(target + 1, vector<vector<int> >());
dp[0].push_back(vector<int>());
for (auto candidate : candidates) {
for (int j = candidate; j <= target; j++) {
if (!dp[j - candidate].empty()) {
auto paths = dp[j - candidate];
for (auto& path : paths)
path.push_back(candidate);
dp[j].insert(dp[j].end(), paths.begin(), paths.end());
}
}
}
return dp[target];
}
};
进阶(Combination Sum II)
进阶版本在第一版的基础上对增加了更多的条件:给出的candidates
数组的元素是可以重复的,所以最大问题就是去重,下面给出递归DFS的做法:
class Solution {
private:
int size;
void dfs(vector<vector<int>>& res, vector<int>& path,
vector<int>& candidates, int begin, int target) {
if (target == 0) {
res.push_back(path);
} else if (begin < size) {
for (int i = begin; i < size; i++) {
if (candidates[i] <= target) {
if (i > 0 && i > begin &&
candidates[i] == candidates[i - 1])
continue; //去重的关键过滤步骤:跳过所有相同的元素
path.push_back(candidates[i]);
dfs(res, path, candidates,
i + 1, target - candidates[i]);
path.pop_back();
} else {
return;
}
}
}
}
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
this->size = candidates.size();
vector<vector<int>> res;
vector<int> path;
dfs(res, path, candidates, 0, target);
return res;
}
};
[Leetcode] Combination Sum 系列的更多相关文章
- Leetcode 之 Combination Sum系列
39. Combination Sum 1.Problem Find all possible combinations of k numbers that add up to a number n, ...
- [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 Combination Sum III
原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...
- LeetCode: Combination Sum I && II && III
Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- LeetCode:Combination Sum I II
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
随机推荐
- IP地址转换32为长整型
Programming Question: Convert an IPv4 address in the format of null-terminated C string into a 32-bi ...
- MATLAB串口操作和GUI编程
程序说明 V1.0 2015/2/08 MATLAB串口操作和GUI编程 概述 本文介绍了程序AD9512_Serial_GUI的编程思路和功能.该程序设计到MATLAB的图像用户界面编程的基 ...
- css实现 显示一行文字,超出用...代替
overflow:hidden; white-space:nowrap; text-overflow:ellipsis;
- 秒杀多线程第七篇 经典线程同步 互斥量Mutex(续)
java使用Synchronized关键字实现互斥,而同时有Lock支持. 这两个的效果是等同的,Synchronized性能的起伏较大,而lock比较收敛. 为了代码的可读性,Synchronize ...
- Day22-CSRF跨站请求伪造
csrf 跨站请求伪造 一.简介 django为用户实现防止跨站请求伪造的功能,通过中间件 django.middleware.csrf.CsrfViewMiddleware 来完成. 1.1 第1次 ...
- Mybatis笔记三:iBatis与MyBatis区别
转载:http://www.tuicool.com/articles/auaAru iBatis 框架的主要优势: 1.iBatis 封装了绝大多数的 JDBC 样板代码,使得开发者只需关注 SQL ...
- Linux 内核分析第八周学习笔记
Linux 内核分析第八周学习笔记 zl + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-10 ...
- Redis Scan迭代器遍历操作原理(二)
续上一篇文章 Redis Scan迭代器遍历操作原理(一)–基础 ,这里着重讲一下dictScan函数的原理,其实也就是redis SCAN操作最有价值(也是最难懂的部分). 关于这个算法的源头,来自 ...
- VS2010中使用Github进行版本控制【补充】
http://blog.csdn.net/softwave/article/details/8499264 前面我在文章<VS2010结合Github进行版本控制>中介绍了VS 2010中 ...
- 【OpenCV】SIFT原理与源码分析
SIFT简介 Scale Invariant Feature Transform,尺度不变特征变换匹配算法,是由David G. Lowe在1999年(<Object Recognition f ...