Combination Sum

Total Accepted: 17319 Total
Submissions: 65259My Submissions

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]

题意:给定一组数C和一个数值T,在C中找到全部总和等于T的组合。

C中的同一数字能够拿多次。找到的组合不能反复。

思路:dfs

每一层的第i个节点有  n - i 个选择分支

递归深度:递归到总和大于等于T就能够返回了

复杂度:时间O(n!)。空间O(n)

感觉測试数据有问题,我用以下两个代码。对于[1,1],1这个输入。输出的结果各自是[[1],[1]]和[[1]],但两个代码都 Accepted 了。我感觉第二个代码才是正确的,输出结果没反复。

//代码一
vector<vector<int> > res;
vector<int> _nums;
void dfs(int target, int start, vector<int> &path){
if(target == 0) res.push_back(path);
for(int i = start; i < _nums.size(); ++i){
if(target < _nums[i]) return ; //这里假设没剪枝的话会超时
path.push_back(_nums[i]);
dfs(target - _nums[i], i, path);
path.pop_back();
}
} vector<vector<int> >combinationSum(vector<int> &nums, int target){
_nums = nums;
sort(_nums.begin(), _nums.end());
vector<int> path;
dfs(target, 0, path);
return res;
}

//代码二
vector<vector<int> > res;
vector<int> _nums;
void dfs(int target, int start, vector<int> &path){
if(target == 0) res.push_back(path);
int previous = -1;
for(int i = start; i < _nums.size(); ++i){
if(_nums[i] == previous) continue;
if(target < _nums[i]) return ; //这里假设没剪枝的话会超时
previous = _nums[i];
path.push_back(_nums[i]);
dfs(target - _nums[i], i, path);
path.pop_back();
}
} vector<vector<int> >combinationSum(vector<int> &nums, int target){
_nums = nums;
sort(_nums.begin(), _nums.end());
vector<int> path;
dfs(target, 0, path);
return res;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Leetcode dfs Combination Sum的更多相关文章

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

  2. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  3. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

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

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

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

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

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

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

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

  8. 从Leetcode的Combination Sum系列谈起回溯法

    在LeetCode上面有一组非常经典的题型--Combination Sum,从1到4.其实就是类似于给定一个数组和一个整数,然后求数组里面哪几个数的组合相加结果为给定的整数.在这个题型系列中,1.2 ...

  9. Leetcode dfs Combination SumII

    Combination Sum II Total Accepted: 13710 Total Submissions: 55908My Submissions Given a collection o ...

随机推荐

  1. js页面载入特效如何实现

    js页面载入特效如何实现 一.总结 一句话总结:可以加选择器(里面的字符串)作为参数,这样函数就可以针对不同选择器,就很棒了. 1.特效的原理是什么? 都是通过标签的位置和样式来实现特效的. 二.js ...

  2. 中小研发团队架构实践之RabbitMQ快速入门及应用

    原文:中小研发团队架构实践之RabbitMQ快速入门及应用 使用过分布式中间件的人都知道,程序员使用起来并不复杂,常用的客户端API就那么几个,比我们日常编写程序时用到的API要少得多.但是分布式中间 ...

  3. Xcode5新特性之注释

    Xcode5新特性之注释 Xcode5在注释式文档方面也有进步,越来越象javadoc. Xcode4 参考一下教程 http://blog.chukong-inc.com/index.php/201 ...

  4. IfSpeed 带宽计算

    http://www.360doc.com/content/11/0304/22/2614615_98214710.shtml http://www.cisco.com/support/zh/477/ ...

  5. 【搜索引擎Jediael开发笔记】v0.1完整代码 2014-05-26 15:17 463人阅读 评论(0) 收藏

    详细代码请见 E:\Project\[重要]归档代码\SearchEngine归档代码 或 https://code.csdn.net/jediael_lu/jediael/tree/10991c83 ...

  6. Android NDK开发之Jni的数据类型

    在前面的一篇博客<Android NDK开发简介>,我简单地说明了Android NDK开发的流程,以及其重要的一环:JNI层得开发.今天我再详细说明一下自己的学习经验. JNI是Java ...

  7. JAVA生成二维码并转化为二进制在页面展示

    引用jar包: zxing的core包,下载路径可参考:http://download.csdn.net/detail/sxg0205/9461106 java代码: 引用包: import java ...

  8. Spring学习笔记之六(数据源的配置)

    1.前言 上一篇博客分析了,Spring中实现AOP的两种动态代理的机制,以下这篇博客.来解说一下Spring中的数据源的配置.  2.DAO支持的模板类 Spring提供了非常多关于Dao支持的模板 ...

  9. ActivityManagerService的启动过程

    AMS对象随系统进程启动而构建,随着系统进程退出而消亡,可以说,AMS与系统进程共存亡. 先上一张总的启动时序图: 上图分为三个步骤: 初始化系统进程的运行环境; 初始化AMS对象; AMS对象启动的 ...

  10. Linux初接触随笔02

    刚开始把Linux既拿来用,又拿来学习怎么用,感觉真不顺手,手上应该有一本实体书籍,专门拿来学习用会好点,但是我现在手头没有,又把系统作为日常使用,只能说太不友好了,坚持不住了,以后等需要用的时候再弄 ...