[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 ...
随机推荐
- kali linux之无线渗透(续)
Airolib 设计用于存储ESSID和密码列表,计算生成不变的PMK(计算资源消耗型) PMK在破解阶段被用于计算PTK(速度快,计算资源要求少) 通过完整性摘要值破解密码SQLite3数据库存储数 ...
- android 开发 简单的小计算器
↑大致效果 项目构成: 随便写的,用的线性布局 activity_main.xml <?xml version="1.0" encoding="utf-8" ...
- [AIR] AS3操作文件时报SecurityError: fileWriteResource 错的解决方法
在用File操作(移动,删除等)或者写入文件时,以下写法会报错 var file:File =File.applicationDirectory.resolvePath("1.swf&quo ...
- Java初学者的学习路线建议
java学习这一部分其实也算是今天的重点,这一部分用来回答很多群里的朋友所问过的问题,那就是我你是如何学习Java的,能不能给点建议?今天我是打算来点干货,因此咱们就不说一些学习方法和技巧了,直接来谈 ...
- Laravel 的核心概念
工欲善其事,必先利其器.在开发Xblog的过程中,稍微领悟了一点Laravel的思想.确实如此,这篇文章读完你可能并不能从无到有写出一个博客,但知道Laravel的核心概念之后,当你再次写起Larav ...
- python中的类方法、静态方法、对象方法
注:以下都是以公有为前提,私有方法只能在类内部调用,不需多讲. 1.对象方法 这种方法都有一个默认参数:self 这代表实例的这个对象 def __init__(self): print(" ...
- leetcode-350-Intersection of Two Arrays II(求两个数组的交集)
题目描述: Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, ...
- Spring注入方式(1)
Spring支持3种依赖注入方式,分别为属性注入.构造器注入和工厂方法注入(很少使用,不推荐),下面分别对属性注入和构造器注入详细讲解. 1.常量注入 属性注入是通过setter方法注入Bean的属性 ...
- git配置教程
一.配置ssh 1.检查本机是否有ssh key设置 如果没有则提示: No such file or directory 如果有则进入~/.ssh路径下(ls查看当前路径文件,rm删除所有文件) 2 ...
- 题解 p2420 让我们异或吧
传送门 #include<iostream> #include<cstdio> #include<cstring> using namespace std; ;in ...