Given a set of candidate numbers (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(回溯)的更多相关文章

  1. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  2. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  3. [Leetcode][Python]39: Combination Sum

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 39: Combination Sumhttps://oj.leetcode. ...

  4. LeetCode题解39.Combination Sum

    39. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T ...

  5. 39. Combination Sum - LeetCode

    Question 39. Combination Sum Solution 分析:以candidates = [2,3,5], target=8来分析这个问题的实现,反向思考,用target 8减2, ...

  6. 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV

    ▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...

  7. [LeetCode] 39. Combination Sum 组合之和

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  8. 【LeetCode】39. Combination Sum (2 solutions)

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  9. [LeetCode] Combination Sum 回溯

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

随机推荐

  1. 【RF库Collections测试】Get Dictionary Values

    Name:Get Dictionary ValuesSource:Collections <test library>Arguments:[ dictionary ]Returns val ...

  2. Linux echo 命令

    echo命令用于输出指定的字符串,常见用法如下: [root@localhost ~]$ echo # 输出一个空白行[root@localhost ~]$ echo "hello worl ...

  3. PL/SQL编程1-基础

    编写第一个存储过程 create or replace procedure test_pro1 is begin ','zydev'); end; / 查看错误 show error 执行存储过程 e ...

  4. cocos2d-x游戏引擎核心之十二——3.x新特性

    v3.0 亮点 使用 C++(C++11) 的特性取代了 Objective-C 的特性 优化了 Labels 优化了渲染器(比 v2.2 更快) 新的事件分发机制 物理引擎集成 新的 UI 对象 J ...

  5. piblog 0.1

    搭建开发环境首先,确认系统安装的Python版本是2.7.x. $ python --version Python 然后,安装开发Web App需要的第三方库:$ sudo pip install j ...

  6. stringstream读入每行数据

    做了下阿里的编程测试题,就30分钟,不是正常的输入输入,直接给一个数组作为输入. 于是带想题和处理数据花了20分钟,最后10分钟搞一个dij模版, 竟然只过了66%,应该是我数组开小了. 题目数据量没 ...

  7. 如何偷懒地用 PHP 搭建一个班级网站

    版权声明:本文由李宜东原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/116 来源:腾云阁 https://www.qclo ...

  8. LightOJ 1348(Aladdin and the Return Journey )

    题目链接:传送门 题目大意:一棵无根树,每个点上有权值,两种操作,0 x y询问x~y路径上权值和 1 x y将 节点 x 权值变为y.对于询问操作输出答案. 题目思路:树链剖分 #include & ...

  9. Centos7 安装zabbix3.0 服务端 详细

    参考: https://www.cnblogs.com/37yan/p/6879218.html http://blog.csdn.net/hao134838/article/details/5712 ...

  10. 高中生的IT之路-1.3那一幕

    上一篇讲到,当时我认为自己的命运就是小时候上学,长大后外出打工,所以高中毕业后就来到了天津,到爸爸的店铺打工. 我爸的店铺就在天津大学校园里,幸运的是,我人生的转折点也就在此. 刚到店里那段时间,每天 ...