LeetCode: Combination Sum I && II && III
Title:
https://leetcode.com/problems/combination-sum/
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]
思路:基本是DFS的思想。将数组排序之后,每次对当前的这个元素与target比较,看最多能塞进去几个当前的这个元素。
如果数组有相同元素,可以采用注释掉的语句进行去重,或者在递归函数中去重。不去重也不会影响结果。(去掉蓝色的也没有影响,不过蓝色的是为了去重)
注意结束条件: 一般都是index == size(),但这边应该是0 == target
for (int i = (target / candidates[idx]); i >= 0; i--) {
record.push_back(candidates[idx]);
}
用来计算最多压入几个当前元素。
for (int i = (target / candidates[idx]); i >= 0; i--) {
record.pop_back();
searchAns(ans, record, candidates, target - i * candidates[idx], idx + 1,candidates[idx]);
//record.pop_back();
}
弹出栈,再进入递归函数。注意,有可能这个元素就不要压入,所以是i >=0
class Solution {
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sort(candidates.begin(), candidates.end());
/*vector<int>::iterator pos = unique(candidates.begin(), candidates.end());
candidates.erase(pos, candidates.end());*/
vector<vector<int> > ans;
vector<int> record;
searchAns(ans, record, candidates, target, 0,-1);
return ans;
} private:
void searchAns(vector<vector<int> > &ans, vector<int> &record, vector<int> &candidates, int target, int idx, int preValue) {
if (target == 0) {
ans.push_back(record);
return;
}
if ( idx == candidates.size() || candidates[idx] > target || preValue == candidates[idx]) {
return;
}
for (int i = (target / candidates[idx]); i >= 0; i--) {
record.push_back(candidates[idx]);
}
for (int i = (target / candidates[idx]); i >= 0; i--) {
record.pop_back();
searchAns(ans, record, candidates, target - i * candidates[idx], idx + 1,candidates[idx]);
//record.pop_back();
}
}
};
Title:
https://leetcode.com/problems/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.
- 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搜索。与Combination Sum中的不同,每次搜索的备选项是从当前index开始到数组结束的元素。不包括重复元素。
class Solution {
public:
vector<vector<int> > results;
vector<vector<int> > combinationSum2(vector<int> &num, int target) { if (num.empty() || num.size() == 0)
return results;
sort(num.begin(),num.end());
vector<int> result;
combine(num,0,target,result);
return results;
} void combine(vector<int> &num,int startIndex, int target,vector<int> &result){
if (0 == target){
//cout<<"add"<<endl;
results.push_back(result);
return ;
}
if (0 > target)
return ;
for (int i = startIndex; i < num.size(); i++){
if (i > startIndex && num[i] == num[i-1])
continue;
result.push_back(num[i]);
combine(num,i+1,target-num[i],result);
result.pop_back();
}
}
};
Title:
https://leetcode.com/problems/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.
Ensure that numbers within the set are sorted in ascending order.
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]]
class Solution {
public:
vector<vector<int>> combinationSum3(int k, int n) {
vector<vector<int> > results;
if ( k < || n < )
return results;
vector<int> result;
DFS(results,result,,k,n,);
return results;
}
void DFS(vector<vector<int> >& results, vector<int>& result, int index, int k, int target, int preVal){
if (index == k && target == ){
results.push_back(result);
return ;
}
if (index == k || target <= preVal)
return ;
for (int i = preVal+; i < ; i++){
result.push_back(i);
DFS(results,result,index+,k,target-i,i);
result.pop_back();
}
}
};
LeetCode: Combination Sum I && II && III的更多相关文章
- Leetcode 39 40 216 Combination Sum I II III
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- LeetCode:Combination Sum I II
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- combination sum(I, II, III, IV)
II 简单dfs vector<vector<int>> combinationSum2(vector<int>& candidates, int targ ...
- [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 III
原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...
- LeetCode: Combination Sum II 解题报告
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- 子集系列(二) 满足特定要求的子集,例 [LeetCode] Combination, Combination Sum I, II
引言 既上一篇 子集系列(一) 后,这里我们接着讨论带有附加条件的子集求解方法. 这类题目也是求子集,只不过不是返回所有的自己,而往往是要求返回满足一定要求的子集. 解这种类型的题目,其思路可以在上一 ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
随机推荐
- 【BZOJ】【1412】【ZJOI2009】狼和羊的故事
网络流/最小割 一开始我是将羊的区域看作连通块,狼的区域看作另一种连通块,S向每个羊连通块连一条无穷边,每个狼连通块向T连一条无穷边,连通块内部互相都是无穷边.其余是四连通的流量为1的边……然后WA了 ...
- 【技术贴】解决bug mantisbt APPLICATION ERROR #1502 没有找到类别
解决bug mantisAPPLICATION ERROR #1502 没有找到类别 mantisbt出现1502问题解决:引起问题的原因:当提交的问题有分类,此时删除此分类,就会出现下面的情况.问题 ...
- 基于 Eclipse 平台的代码生成技术
------------------------------------------------------------------ 转自http://www.ibm.com/developerwor ...
- 关于struts2如何去掉默认的后缀(.action)
struts2是可以配置默认的后缀名的,如http://localhost:8080/test.action,这个是默认的,但是也可以通过配置去修改这个.action为别的. 这里是通过一个常量配置改 ...
- Mutex vs Semaphore
What are the differences between Mutex vs Semaphore? When to use mutex and when to use semaphore? Co ...
- POJ 3641
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6044 Accepted: 24 ...
- [shell编程]初识sed和gawk
一.sed编辑器 shell脚本最常见的用途就是处理文本文件,sed和gawk能够极大的简化需要进行的数据处理任务.sed编辑器是流编辑器,跟普通交互式文本编辑器(如vim)不同.流编辑器 ...
- Javascript实现两张图片的延迟加载
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- eclipse 或MyEclipse将工程进行移动的时候会对@Override报错的处理方法
有时候导入javaSE,javaEE,android 工程的时候,明明是刚刚用过的没有问题的工程,但重新导入的时候就报错. 提示The method ... must override a sperc ...
- 深入浅出Java并发包—锁机制(二)
接上文<深入浅出Java并发包—锁机制(一) > 2.Sync.FairSync.TryAcquire(公平锁) 我们直接来看代码 protected final boolean tr ...