Combination Sum

Given a set of candidate numbers (C) (without duplicates) 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.
  • 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的思想来解题。

数组从头到尾,每次决定是否将当前的值加入组合:

  • 如果是,那么判断当前组合是否sum == target,

    • 等于——>将该组合加入解集合,return;
    • 不等于——>继续递归
  • 如果不是,那么index+1(数组向后移动),继续递归

代码非常容易理解,如下所示:

class Solution {
public:
vector<vector<int>> re;
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<int> temp;
search(candidates,temp,0,candidates.size(),target,0);
return re;
}
void search(vector<int> candidates,vector<int> temp,int index,int len,int target,int cur){ if(index >= len || cur > target) return; //do not select this value,search deepper,index need increase
search(candidates,temp,index+1,len,target,cur); /*
* select this value,and judge if it equals the target.
* If yes,push back the vector and return,if not,search continue
* index don't change,because the number can be repeated
*/
temp.push_back(candidates[index]);
cur += candidates[index];
if(cur == target){
re.push_back(temp);
return;
}
else{
search(candidates,temp,index,len,target,cur);
}
}
};

[LeetCode]Combination Sum题解(DFS)的更多相关文章

  1. LeetCode Combination Sum III

    原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...

  2. [Leetcode] Combination Sum 系列

    Combination Sum 系列题解 题目来源:https://leetcode.com/problems/combination-sum/description/ Description Giv ...

  3. LeetCode: Combination Sum I && II && III

    Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...

  4. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

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

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

  6. [LeetCode] Combination Sum III 组合之和之三

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

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

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

  8. [LeetCode] Combination Sum 组合之和

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

  9. LeetCode: Combination Sum II 解题报告

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

随机推荐

  1. 现在就启用 HTTPS,免费的!

    现在就启用 HTTPS,免费的! 现在,你应该能在访问https://konklone.com的时候,在地址栏里看到一个漂亮的小绿锁了,因为我把这个网站换成了HTTPS协议.一分钱没花就搞定了. 为什 ...

  2. java使用Redis8--3.0集群

    Redis集群至少需要3个主节点 # cd /usr/redis 创建一个目录 # mkdir cluster # cd cluster 1.复制一个配置文件 # cp ../redis.conf 9 ...

  3. 【微信小程序】——wxss引用外部CSS文件及iconfont

    小程序引入外部文件的方式是:@import "*/*.wxss"; 因为业务需要,正在开发的小程序中需要使用iconfont,很容易想到了H5的引入方式: ```` @font-f ...

  4. 【JavaScript】类继承(对象冒充)和原型继承__深入理解原型和原型链

    JavaScript里的继承方式在很多书上分了很多类型和实现方式,大体上就是两种:类继承(对象冒充)和原型继承. 类继承(对象冒充):在函数内部定义自身的属性的方法,子类继承时,用call或apply ...

  5. 嵌入式C语言自我修养 05:零长度数组

    5.1 什么是零长度数组 顾名思义,零长度数组就是长度为0的数组. ANSI C 标准规定:定义一个数组时,数组的长度必须是一个常数,即数组的长度在编译的时候是确定的.在ANSI C 中定义一个数组的 ...

  6. BiliBili, ACFun… And More!【递归算法】

    题源:http://acm.uestc.edu.cn/#/problem/show/3 题解: 题意:播放一段视频文件,有播放速度和缓冲速度两种,因为作者的癖好,播放前要缓冲几秒钟(这段时间不计算在总 ...

  7. Maven自动FTP远程部署

    参照官网文档: https://maven.apache.org/plugins/maven-deploy-plugin/examples/deploy-ftp.html 1.在pom.xml中加入: ...

  8. Windows7上用VS编译本地使用的live555

    本文链接:https://www.jianshu.com/p/6ea100865744 环境 系统:Windows7 SP1 64位 编辑器:Visual Studio Community 2017 ...

  9. 安装php readline扩展报错 Please reinstall libedit

    现象:configure: error: Please reinstall libedit – I cannot find readline.h解决办法:安装 Editline Library (li ...

  10. transform旋转变换效果

    div{ transform:rotate(7deg); -ms-transform:rotate(7deg); /* IE 9 */ -moz-transform:rotate(7deg); /* ...