[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 ...
随机推荐
- Modbus通用数据读取工具设计及使用
一.公共功能码定义 二.能读取的数据类型 1.bit类型,比如01功能码,读到的就是位的状态,是ON 还是OFF,也就是对应着0或1. 2.byte类型,比如03功能码. 3.short类型,比如03 ...
- day01 --class --home
# 1.简述变量命名规范# 2.name = input(“>>>”) name变量是什么数据类型?# 3.if条件语句的基本结构? # 4.用print打印出下面内容:# ⽂能提笔 ...
- 【OpenCV3】直线拟合--FitLine()函数详解
一.FitLine()函数原型 CV_EXPORTS_W void fitLine( InputArray points, // 待输入点集(一般为二维数组或vector点集) OutputArray ...
- Hyperledger Fabric
https://wiki.hyperledger.org/display/fabric Hyperledger Fabric 转至元数据结尾 由 Tracy Kuhrt创建, 最终由 Da ...
- ubuntu下安装ffmpeg
sudo add-apt-repository ppa:kirillshkrogalev/ffmpeg-next sudo apt-get update sudo apt-get install ff ...
- windows安装tesseract-OCR及使用
tesseract是Python的一个OCR(光学字符识别)库 首先下载tesseract的exe安装文件 https://github.com/UB-Mannheim/tesseract/wik ...
- [温故]图解java多线程设计模式(一)
去年看完的<图解java多线程设计模式>,可惜当时没做笔记,导致后来忘了许多东西,打算再温习下这本书,顺便在这里记录一下~ 1.顺序执行.并行.并发 顺序执行:多个操作按照顺序依次执行. ...
- pytest文档博客链接
关于pytest的博客: https://www.cnblogs.com/yoyoketang/tag/pytest/default.html?page=2
- po'j2559 Largest Rectangle in a Histogram 单调栈(递增)
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29498 ...
- Tomcat *的下载(绿色版和安装版都适用)
不多说,直接干货! 1.先下载tomcat,到http://tomcat.apache.org/ 2.注意:下载可以下载zip格式或exe格式的,其中zip格式的只要解压缩再配置下环境变量就可以使用了 ...