39. Combination Sum
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.
For example, given candidate set [2, 3, 6, 7] and target 7,
A solution set is:
[
[7],
[2, 2, 3]
]
在所给的正数数组(不含重复元素)中,找出所有的元素组合使得它们的和等于目标值。这些找到的元素组合要以非递减顺序排列,这些组合不能够是重复的,但数组中元素可以重复使用。
看一下题目给出的例子:看看例子:[2,3,6,7], 7
搜索过程:
从2开始进行搜索,由于允许重复:
2,2,2
接续重复2,和为8,大于7,所以退一步再从下一个数开始搜:
2,2
2,2,3
此时和=7,符合要求。后续的搜索:
2,2,6 不符
2,2,7 不符
2,3,3 不符
2,3,6 不符
2,3,7 不符
3,3,3 不符
3,3,6 不符
3,3,7 不符
......
6,7 不符
7 符合
通过这样的搜索过程就得到了需要的元素组合。思路是非常朴素的穷举,一个个尝试,试错了就退回——所以这里我们使用回溯法。
另写一个递归函数,加入三个变量,res保存所有已经得到的解,resItem是其中一个目标值组合解,index是当前搜索范围的起点。每次调用新的递归函数时,target减去当前数组元素:
vector<vector<int> > combinationSum(vector<int> &candidates, int target){
vector<vector<int>> rs;
if (candidates.size() == 0) return rs;
sort(candidates.begin(),candidates.end());
vector<int> resItem;
dfs(candidates,rs,resItem,0,target);
return rs;
}
//cdd是原数组,res是目标值组合集合,resItem是其中一个目标值组合解,index是当前搜索范围的起点,target是目标值
void dfs(vector<int> &cdd ,vector<vector<int>> &res ,vector<int> &resItem ,int index ,int target){
if (target == 0) {//表明已经找到所求组合
res.push_back(resItem);
return;
}
if (target < cdd[index]) return;//此时sum必定大于target
for (int i = index ; i < cdd.size(); ++i){
resItem.push_back(cdd[i]);
dfs(cdd,res,resItem,i,target-cdd[i]);
resItem.pop_back();//回溯
}
}
9ms通过。
第一版代码,没有用递归,15ms。
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
int cur = candidates.size() - 1;
vector<vector<int>> rs;
while (cur >= 0)
{
int remain = target - candidates[cur];
if (remain == 0)
{
rs.push_back({ candidates[cur] });
}
else if (remain > 0)
{
auto beg = candidates.begin();
vector<int> subCandidates = vector<int>(beg, beg + cur + 1);
vector<vector<int>> temp = combinationSum(subCandidates, remain);
if (temp.size() != 0) {
for (auto &item : temp) {
item.push_back(candidates[cur]);
rs.push_back(item);
}
}
}
--cur;
}
return rs;
}
40. Combination Sum II
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.
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]
]
本质没有区别,只是之前那道题给定数组中的数字可以重复使用,而这道题不能,而且这道题给定数组中可能有重复数字。所以改动的地方有两处:1.递归调用里的index传参要改为i+1;2.在递归的for循环里跳过数组中重复项。
vector<vector<int> > combinationSum2(vector<int> &candidates, int target){
vector<vector<int>> rs;
if (target <= 0 || candidates.size() == 0) return rs;//因为数组元素都是正数,所以目标值=0时必定找不到
sort(candidates.begin(),candidates.end());
vector<int> resItem;
dfs(candidates,rs,resItem,0,target);
return rs;
}
//cdd是原数组,res是目标值组合集合,resItem是其中一个目标值组合,index是当前搜索范围的起点,target是目标值
void dfs(vector<int> &cdd ,vector<vector<int>> &res ,vector<int> &resItem ,int index ,int target){
if (target == 0) {//表明已经找到所求组合
res.push_back(resItem);
return;
}
if (target < cdd[index]) return;//此时sum必定大于target
for (int i = index ; i < cdd.size(); ++i){
if (i > index && cdd[i] == cdd[i-1]) continue;
resItem.push_back(cdd[i]);
dfs(cdd,res,resItem,i+1,target-cdd[i]);
resItem.pop_back();//回溯
}
}
第一版代码:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<vector<int>> rs;
test(candidates, target, rs);
return rs;
}
void test(vector<int>& candidates, int target, vector<vector<int>>&rs) {
int cur = candidates.size() - 1;
while (cur >= 0)
{
int remain = target - candidates[cur];
if (remain == 0)
{
rs.push_back({ candidates[cur] });
}
else if (remain > 0)
{
auto beg = candidates.begin();
vector<int> subCandidates = vector<int>(beg, beg + cur);
vector<vector<int>> temp;
test(subCandidates, remain, temp);
if (temp.size() != 0) {
for (auto &item : temp){
item.push_back(candidates[cur]);
rs.push_back(item);
}
}
}
while (cur > 0 && candidates[cur] == candidates[cur - 1])
{
--cur;
}
--cur;
}
}
216. Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.
Example 1:
Input: k = 3, n = 7
Output: [[1,2,4]]
Example 2:
Input: k = 3, n = 9
Output:
[[1,2,6], [1,3,5], [2,3,4]]
这道题跟之前两道有点不太一样。从1~9这九个数字中挑选k个不重复的数,使得它们的和等于n。但本质上还是差不多的。
vector<vector<int>> combinationSum3(int k, int n) {
if (k > 9 || n < k*(k+1)/2 || n > (9-k + 1 + 9)*k/2) return{}; //(首项+末项)*项数/2
vector<vector<int>> rs;
vector<int> resItem;
cs3dfs(rs,resItem,1,k,n);
return rs;
}
void cs3dfs(vector<vector<int>> &res ,vector<int> &resItem ,int index , int k ,int n){
if (n == 0 && resItem.size() == k) {res.push_back(resItem); return;}
if (n < index) return;
if (resItem.size() < k){
for (int i = index ;i <= 9 ; ++i){
resItem.push_back(i);
cs3dfs(res,resItem,i+1,k,n-i);
resItem.pop_back();
}
}
}