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 ...
随机推荐
- bitmap位图法
位图法定义 位图法就是bitmap的缩写,所谓bitmap,是用每一位来存放某种状态,适用于大规模数据,但数据状态又不是很多的情况.通常是用来判断某个数据存不存在的. 例如,要判断一千万个人的状态,每 ...
- GSM/GPRS/3G/4G
1.状态机机制的gprs拨号 像GPRS/3G模块之类的应用,需要连接,登陆,初始化等步骤完成后才能传输数据,而这些步骤又比较耗时. 所以用 状态机 + 超时 的机制来实现比较合理. 如下代码片段来描 ...
- Python(调用函数、定义函数)
函数的返回值: return 值:只能返回一次,只要执行return函数就终止 返回值:没有类型限制,也没有个数限制 没有return:None 返回一个值 返回多个值:元组 先定义,后使用,定义阶段 ...
- java注解1
http://computerdragon.blog.51cto.com/6235984/1210969 http://blog.csdn.net/it_man/article/details/440 ...
- IO 复习笔记
输入流,从源到流中:输出流,从流到目的地. 1. 操作文件: 1).写入:FileOutputStream或者FileWriter 2).读取:FileInputStream或者Fil ...
- hadoop20---代理另一种方式
package cn.itcast_05_proxy.service; /** * 这是一个业务的接口,这个接口中的业务就是返回衣服的价格 */ public interface IBoss {//接 ...
- Spark机器学习7·降维模型(scala&python)
PCA(主成分分析法,Principal Components Analysis) SVD(奇异值分解法,Singular Value Decomposition) http://vis-www.cs ...
- Apache 配置参数
参数说明 1.Global Environment 全局环境配置,决定Apache服务器的全局参数3.Virtual Hosts—虚拟主机,虚拟主机不能与Main Server主服务器共存,当启用了虚 ...
- ios 中实现storyboard 与xib 之间的切换
1,跳转到xib 假设有一个按钮,这个按钮就是实现跳转的,那么在这个按钮的点击事件中,代码可以这样写. AViewController *a1= [[AViewController alloc]ini ...
- 20165101刘天野 2018-2019-2《网络对抗技术》Exp1 逆向与Bof基础
20165101刘天野 2018-2019-2<网络对抗技术>Exp1 逆向与Bof基础 1. 逆向及Bof基础实践说明 1.1 实践目标 本次实践的对象是一个名为pwn1的linux可执 ...