LeetCode OJ: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,只不过在在有限搜索的过程中用到了剪枝,使得在优先搜索的过程中一旦遇到了对应的值那么就返回,
不再搜索余下节点。所以这题,首先将数组排序,排序之后再使用DFS加剪枝就可以达到目标。代码如下:
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
tmpCdd = candidates;
sort(tmpCdd.begin(), tmpCdd.end());
this->target = target;
vector<int> tmpVec;
dfs(, tmpVec);
return result;
}
private:
int target;
vector<int> tmpCdd;
vector<vector<int>> result;
private:
void dfs(int index, vector<int> & tmpVec)
{
if(index == tmpCdd.size()) return; //到达叶节点
int tmpSum = accumulate(tmpVec.begin(), tmpVec.end(), );
if(tmpSum == target){
result.push_back(tmpVec);
return;
}else if(tmpSum > target){//剪枝
return;
}else{
for(int i = index; i < tmpCdd.size(); ++i){//这里从i开始的原因是因为参数可以是重复的
tmpVec.push_back(tmpCdd[i]);
dfs(i, tmpVec);
tmpVec.pop_back();//回溯
}
}
}
};
java版本的如下所示,思想一样,方法有一点不同,这次不对tmpCdd数组中的值每次都求和,而是每递归一次之后将target的值减去一个数传入 下次递归中,代码如下:
public class Solution {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
for(int i = 0; i < candidates.length; ++i){
ArrayList<Integer> tmpCdd = new ArrayList<Integer>();
tmpCdd.add(candidates[i]);
dfs(i, tmpCdd, candidates, target - candidates[i]);
tmpCdd.remove(tmpCdd.size() - 1);
}
return ret;
}
public void dfs(int index, List<Integer> tmpCdd, int[] candidates, int target){
if(index == candidates.length)
return;
if(target < 0)
return; //直接剪枝
if(target == 0){
ret.add(new ArrayList<Integer>(tmpCdd));
return;
}else{
for(int i = index; i < candidates.length; ++i){
tmpCdd.add(candidates[i]);
dfs(i, tmpCdd, candidates, target - candidates[i]);
tmpCdd.remove(tmpCdd.size() - 1);
}
}
}
}
LeetCode OJ:Combination Sum (组合之和)的更多相关文章
- [LeetCode] 39. Combination Sum 组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- [leetcode]39. Combination Sum组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- [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(组合总和)
这道题是LeetCode里的第39道题. 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组 ...
- [LeetCode] 377. Combination Sum IV 组合之和 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 ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 216. Combination Sum III 组合之和 III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
随机推荐
- JVM虚拟机—JVM内存
JVM在运行时将数据划分为了5个区域来存储,这5个区域图示如下: 其中方法区和堆对是所有线程共享的内存区域:而java栈.本地方法栈和程序员计数器是运行时线程私有的内存区域. 首先我们熟悉一下一个 J ...
- eclipse导入项目,项目名出现红叉的情况(修改版)
转至:http://blog.csdn.net/niu_hao/article/details/17440247 今天用eclipse导入同事发给我的一个项目之后,项目名称上面出现红叉,但是其他地方都 ...
- CRM项目问答总结
1. 通过ChangeList封装好多数据 DA: 在stark组件中,有五个封装的大类: class FilterOption(object): ----用于封装组合搜索的配置信息(数据库字段,是否 ...
- js基本
BOM 浏览器对象模型 DOM 文档对象模型 js主要是来操作DOM和BOM,用的事件驱动方式,通过事件去执行相应函数 如何加载:在html当中有写链接,然后加载的时候会把js函数,数据全取出来,然后 ...
- delphi webbrowser post自动登录
delphi webbrowser post自动登录 var EncodedDataString: WideString; PostData: OleVariant; Headers: ...
- js刷新页面 location.reload()
在javascript编程中,多使用location.reload实现页面刷新. 例子: 代码示例: window.location.href=window.location.href; window ...
- 【c++习题】【17/5/8】重载运算符
1.设计一个Complex(复数)类,完成如下要求: 该类具有实部(Real_Part)和虚部(Image_Part)通过重载运算符“+”实现两个复数的相加通过重载运算符“+”实现一个复数与一个数值的 ...
- Python 函数定义和使用
# 函数的概念 # 概念 # 写了一段代码实现了某个小功能; 然后把这些代码集中到一块, 起一个名字; 下一次就可以根据这个名字再次使用这个代码块, 这就是函数 # 作用 # 方便代码的重用 # 分解 ...
- spring security使用hibernate进行查询数据库验证
前面查询数据库采用的都是jdbc方式,如果系统使用的是hibernate,该如何进行呢,下面就是实现步骤,关键还是实现自定义的UserDetailsService 项目结构如下: 使用hibernat ...
- LeetCode——Sum of Two Integers
LeetCode--Sum of Two Integers Question Calculate the sum of two integers a and b, but you are not al ...