[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 ...
随机推荐
- shell脚本如何获取当前时间
在shell脚本里常常需要获取系统时间来处理某项操作,linux的系统时间在shell里是可以直接调用系统变量的如: 获取今天时期:`date +%Y%m%d` 或 `date +%F` 或 $(da ...
- document.readyState的使用
document.readyState:判断文档是否加载完成.firefox不支持. 这个属性是只读的,传回值有以下的可能: 0-UNINITIALIZED:XML 对象被产生,但没有任何文件被加载. ...
- MySQL5.7初始配置
MySQL5.7初始配置 Windows7 环境安装MySQL5.7配置命令 <<<<<<<<<<<<<<<& ...
- 【刷题】洛谷 P3796 【模板】AC自动机(加强版)
题目描述 有 \(N\) 个由小写字母组成的模式串以及一个文本串 \(T\) .每个模式串可能会在文本串中出现多次.你需要找出哪些模式串在文本串 \(T\) 中出现的次数最多. 输入输出格式 输入格式 ...
- 延长xss的攻击(转)
XSS 的本质仍是一段脚本.和其他文档元素一样,页面关了一切都销毁.除非能将脚本蔓延到页面以外的地方,那样才能获得更长的生命力. 庆幸的是,从 DOM 诞生的那一天起,就已为我们准备了这个特殊的功能, ...
- 51nod1967 路径定向(欧拉回路+结论题)
看到入度等于出度想到欧拉回路. 我们把边都变成无向边,有一个结论是偶数度的点都可以变成出入度相等的点,而奇数点的不行,感性理解分类讨论一下就知道是对的. 还有一个更好理解的结论是变成无向边后奇数点的个 ...
- 【loj2639】[Tjoi2017]不勤劳的图书管理员
#2639. 「TJOI2017」不勤劳的图书管理员 题目描述 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产 ...
- codeforces 217E 【Alien DNA】
倒序考虑每一个操作,对于一个操作$[l, r]$,他产生的影响区间将是$[r+1,r + r + l - 1]$,如果$r+l-1>K$的话,$K$之后的区间我们是不关心的. 暴力扫描这个区间 ...
- OpenCV---色彩空间(二)HSV追踪颜色对象和通道分离与合并
一:HSV追踪有颜色对象 def inRange(src, lowerb, upperb, dst=None) #lowerb是上面每个颜色分段的最小值,upperb是上面每个颜色分段的最大值,都是列 ...
- 日期/时间处理工具 DateTimeUtil
此类是我们项目的 日期/时间处理工具,在此做个记录! /* * Copyright 2014-2018 xfami.com. All rights reserved. * Support: https ...