39. Combination Sum(回溯)
candidates
) (without duplicates) and a target number (target
), find all unique combinations in candidates
where the candidate numbers sums to target
.The same repeated number may be chosen from candidates
unlimited number of times.
Note:
- All numbers (including
target
) will be positive integers. - The solution set must not contain duplicate combinations.
Example 1:
Input: candidates =[2,3,6,7],
target =7
,
A solution set is:
[
[7],
[2,2,3]
]
Example 2:
Input: candidates = [2,3,5],
target = 8,
A solution set is:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
class Solution {
private List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<Integer> temp = new ArrayList<Integer>();
help(temp,candidates,0,0,target);
return res;
}
private void help(List<Integer> temp,int[] nums,int index,int cursum,int target){
if(cursum>target)
return;
if(cursum==target)
res.add(new ArrayList<Integer>(temp));
for(int i = index;i<nums.length;i++){
temp.add(nums[i]);
help(temp,nums,i,cursum+nums[i],target);
temp.remove(temp.size()-1);
}
}
}
2019.3.12
class Solution {
public:
vector<vector<int>> finalres ;
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
if(candidates.size()==) return finalres;
vector<int> curres;
help(,curres,candidates,,target);
return finalres; }
void help(int cursum,vector<int>& curres,vector<int>& candidates,int index,int target){
if(cursum==target)
finalres.push_back(curres);
if(cursum>target)
return;
for(int i = index;i<candidates.size();i++){
curres.push_back(candidates[i]);
help(cursum,curres,candidates,i,target-candidates[i]);
curres.pop_back(); }
}
};
39. Combination Sum(回溯)的更多相关文章
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- [Leetcode][Python]39: Combination Sum
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 39: Combination Sumhttps://oj.leetcode. ...
- LeetCode题解39.Combination Sum
39. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T ...
- 39. Combination Sum - LeetCode
Question 39. Combination Sum Solution 分析:以candidates = [2,3,5], target=8来分析这个问题的实现,反向思考,用target 8减2, ...
- 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV
▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...
- [LeetCode] 39. Combination Sum 组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- 【LeetCode】39. Combination Sum (2 solutions)
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- [LeetCode] Combination Sum 回溯
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
随机推荐
- Nginx(四)-- 配置文件之location
1.location的作用 location主要做定位功能,根据uri来进行不同的定位. 2.location的语法 location [=|~|~*|^~] /uri/ { …} = 开头表示精确匹 ...
- 破解X-Pack和更新许可证
某一天打开 Kibana 对应的 Monitoring 选项卡的时候,发现提示需要下载新的 license,旧的 license 已经过期了: 退出重新登录 发现禁止登录,提示:Login is di ...
- APP的缓存文件放在哪里?
只要是需要进行联网获取数据的APP,都会在本地产生缓存文件.那么,这些缓存文件到底放在什地方合适呢?系统有没有给我们提供建议的缓存位置呢?不同的缓存位置有什么不同呢? 考虑到卸载APP必须删除缓存 在 ...
- 学习C#基础知识这段时间
似乎穿越的感觉,我又来到了这周的周五,总是在周五,知道了时间的概念,上午会讲课,但是在下午就是一个总结小练习,上午老师给我们讲了委托,在听课时间感觉很简单啊,哪里有难的地方啊,一直在好奇,老师在演示给 ...
- PHP常用必备函数
array_change_key_case — 返回字符串键名全为小写或大写的数组 array_chunk — 将一个数组分割成多个 array_combine — 创建一个数组,用一个数组的值作为其 ...
- 以用户名注册来分析三种Action获取数据的方式
1.注入属性 直接注入属性: public String userName; public String getUserName() { return userName; } public void ...
- linux 上安装pstree
linux 无法使用pstree centos7上默认没有安装psmisc包. 1.在 Mac OS上 brew install pstree 2.在 Fedora/Red Hat/CentOS yu ...
- 详解Javascript中prototype属性
转自:https://www.jb51.net/article/91826.htm 在典型的面向对象的语言中,如java,都存在类(class)的概念,类就是对象的模板,对象就是类的实例.但是在Jav ...
- web.xml的contextConfigLocation作用及自动加载applicationContext.xml
web.xml的contextConfigLocation作用及自动加载applicationContext.xml 转自:http://blog.csdn.net/sapphire_aling/ar ...
- windows下resin的配置部署与调试
配置 从Resin官网(http://www.caucho.com)下载Resin解压后,启动Resin,运行resin根目录下的resin.exe文件,运行期间将出现下图所示的命令提示符窗口. 表示 ...