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 (组合之和)的更多相关文章

  1. [LeetCode] 39. Combination Sum 组合之和

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  2. [leetcode]39. Combination Sum组合之和

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  3. [LeetCode] Combination Sum 组合之和

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  4. 【LeetCode】Combination Sum(组合总和)

    这道题是LeetCode里的第39道题. 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组 ...

  5. [LeetCode] 377. Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  6. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  7. [LeetCode] 40. Combination Sum II 组合之和 II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. mysql 建立表之间关系 练习 2

    创建数据库db6 create database db6 charset=utf8; user db6; # 创建班级表 mysql) not null unique); Query OK, rows ...

  2. 图形数据库 Neo4j 开发实战【转载】

    简介: Neo4j 是一个高性能的 NoSQL 图形数据库.Neo4j 使用图(graph)相关的概念来描述数据模型,把数据保存为图中的节点以及节点之间的关系.很多应用中数据之间的关系,可以很直接地使 ...

  3. Java集合(4):Iterator(迭代器)

    迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构.迭代器通常被称为“轻量级”对象,因为创建它的代价小. Java中的Iterator功能比较简单, ...

  4. php 内存泄漏

    所谓内存泄漏是指进称在执行过程中,内存的占有率逐步升高,不释放, 系统所拥有的可用内存越来越少的现象. php-fpm耗光内存,不释放,就是所谓的内存泄漏,内存泄漏对长期运行的程序有威胁,所以应该定期 ...

  5. HDU - 6321 Problem C. Dynamic Graph Matching (状压dp)

    题意:给定一个N个点的零图,M次操作,添加或删除一条边,每一次操作以后,打印用1,2,...N/2条边构成的匹配数. 分析:因为N的范围很小,所以可以把点的枚举状态用二进制表示集合.用一维数组dp[S ...

  6. Linux中top和free命令(6/15)

    top:命令提供了实时的对系统处理器的状态监视.它将显示系统中CPU最“敏感”的任务列表. 该命令可以按CPU使用.内存使用和执行时间对任务进行排序: 而且该命令的很多特性都可以通过交互式命令或者在个 ...

  7. Django 中间件简介

    Django 中间件简介 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. ...

  8. 20145235李涛《网络对抗》Exp6 信息搜集与漏洞扫描

    基础问答 那些组织负责DNS,IP的管理? 全球根服务器均由美国政府授权的ICANN统一管理,负责全球的域名根服务器.DNS和IP地址管理.全球根域名服务器:绝大多数在欧洲和北美(全球13台,用A~M ...

  9. PHP7的五大新特性

    如果你使用的是基于 composer 和 PSR-4 的框架,这种写法是否能成功的加载类文件?其实是可以的,composer 注册的自动加载方法是在类被调用的时候根据类的命名空间去查找位置,这种写法对 ...

  10. OwinStartup not firing

    https://stackoverflow.com/questions/20203982/owinstartup-not-firing 缺少依赖 Make sure you have installe ...