[LeetCode]Combination Sum题解(DFS)
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 target7
,
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)的更多相关文章
- LeetCode Combination Sum III
原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...
- [Leetcode] Combination Sum 系列
Combination Sum 系列题解 题目来源:https://leetcode.com/problems/combination-sum/description/ Description Giv ...
- LeetCode: Combination Sum I && II && III
Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [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 II 解题报告
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
随机推荐
- 操作mysql的指令
1,通过ip,端口,用户名,密码登陆数据 命令格式为:mysql -h ip -u root -p -P 3306例如:mysql -h 127.0.0.1 -u root -p -P 3306 2, ...
- 双绞线的制作(常用568B)
EIA/TIA的布线标准中规定了两种双绞线的线序568A与568B 标准568A: 绿白—1 绿—2 橙白—3 蓝—4 蓝白—5 橙—6 棕白—7 棕--8 标准568B: 橙白—1 ...
- java后台简单从腾讯云下载文件通知前端以附件的形式保存
腾讯云对象存储和阿里云差不多 这是我的配置 /** * 腾讯云client * @return COSClient */ public static COSClient getCOSClient() ...
- 空行会影响 Java 编译吗?
简评:往往越简单的问题越容易被人们忽略. 问题 这个月的 Stack Overflow 有篇热门文章是国外有位开发者提出: 当我仅仅对 Java 类增加了一行空行,为什么编译后得到了两个不同的字节码文 ...
- 晦涩难懂的shell命令
初学shell脚本,过程中发现许多不易于理解的脚本语言,网上各种查找学习之后,择优精简一番,做出以下总结,方便以后遗忘了回顾,也为像我一样的初学者提供方便——推荐给初学者的一本书:<Linux ...
- 编译原理(六)自底向上分析之LR分析法
自底向上分析之LR分析法 说明:以老师PPT为标准,借鉴部分教材内容,AlvinZH学习笔记. 基本概念 1. LR分析:从左到右扫描(L)自底向上进行规约(R),是规范规约,也即最右推导(规范推导) ...
- QuantLib 金融计算——基本组件之 DateGeneration 类
目录 QuantLib 金融计算--基本组件之 DateGeneration 类 QuantLib 金融计算--基本组件之 DateGeneration 类 许多产品的估值依赖于对未来现金流的分析,因 ...
- Mysql update from
UPDATE tab1 a INNER JOIN tab_game_version as b ON a.id=b.id SET a.advert_data=0 where a.advert_dat ...
- jenkins+appium android app自动化测试
jenkins安装 pytest+jenkins安装+allure报告 新建任务 其他默认,保存 立即构建 test_login.py from src.pages import login_page ...
- ator自动生成mybatis配置和类信息
generator自动生成mybatis的xml配置.model.map等信息: 1.下载mybatis-generator-core-1.3.2.jar包. 网址:http://cod ...