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 ...
随机推荐
- Android Wear - Design Principles for Android Wear(设计原则)
---------------------------------------------------------------------------------------------------- ...
- Dora.Interception, 为.NET Core度身打造的AOP框架[3]:Interceptor的注册
在<不一样的Interceptor>中我们着重介绍了Dora.Interception中最为核心的对象Interceptor,以及定义Interceptor类型的一些约定.由于Interc ...
- lucene实现初级搜索引擎
一.系统设计 搜索引擎项目代码主要分为三个部分,第一部分是构建索引,全文检索:第二部分是输入问题,对问题进行分词.提取关键词.关键词扩展:第三部分是将搜索结果输出到GUI图形用户界面. 二.搜索引擎 ...
- begoo——对象的CRUD操作
如果已知主键的值,那么可以使用这些方法进行CRUD操作 对object操作的四个方法Read/Insert/Update/Delete o := orm.NewOrm() user := new(Us ...
- java.text.SimpleDateFormat使用介绍
java.text.SimpleDateFormat的使用 java.lang.Object | +----java.text.Format | +-- ...
- Django:学习笔记(2)——创建第一个应用
Django:学习笔记(2)——创建第一个应用 创建应用 在 Django 中,每一个应用都是一个 Python 包,并且遵循着相同的约定.Django 自带一个工具,可以帮你生成应用的基础目录结构, ...
- kubeadm方式安装kubernetes
系统: Ubuntu 18.04.2 LTS 内存: 8G 机器: 属性 IP Hostname ssh Master 192.168.91.48 blackray-pc node1 1 ...
- Kattis - pizzahawaii 【状态标记】
Kattis - pizzahawaii [状态标记] Description You are travelling in a foreign country. Although you are al ...
- UVALive 6911 F - Double Swords
思路:1.把所有有长度的剑看做点.Ai点是肯定要取.然后求另一把剑. 先对右区间排个序,然后每次看这个区间范围内有没有剑,如果没有就添加一把(值为右端点的剑): 如果有并且数量为1且这条龙的Ai等这把 ...
- Spring 之通过 Java 代码装配 bean
[关于IoC的几点认识] 1.面向接口编程 --> 每层只向上层提供接口 2.inversion of control (IoC) -->参考百度百科 3.DI是IoC的一种实现方式 [ ...