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. 什么原因接触接触impala的

    最近一个项目,关于大数据的改造项目,底层选择Impala还是sparkSQL呢? 最后选择Impala.这样就开启了我的Impala学习之旅.我大部分负责Imapa接口开发工作. 我是控制不住的想整个 ...

  2. Nutch 使用总结

    Nutch 是一个开源Java 实现的搜索引擎.它提供了我们运行自己的搜索引擎所需的全部工具.包括全文搜索和Web爬虫. Nutch使用方法简介: http://blog.csdn.net/pengp ...

  3. MySQL主从复制与读写分离[修改]

    作者:lixiuran 日期:2014年5月2日   备注[本人根据网上资源修改,参考http://www.cnblogs.com/luckcs/articles/2543607.html] 测试环境 ...

  4. OpenGL超级宝典总结(二)2D/3D笛卡尔坐标、坐标裁剪、纹理坐标、MVP转换等概念

    如果你想把图形渲染在正确的位置上,那么坐标的设置就很重要了.在OpenGL中,与坐标相关的主要有笛卡尔坐标.坐标裁剪.纹理坐标.MVP(Model View Projection)转换. 1.笛卡尔坐 ...

  5. php学习四:数组(一)

    1.  直接赋值方式: ①   索引数组:以索引来存储数据,内存不是连续的,但是js中的内存是连续的 代码如下: $array[0] = "11"; $array[1] = &qu ...

  6. ubuntu下code::blocks编译运行一个简单的gtk+2.0项目

    在具体的操作之前,首先需要安装一些必要的软件.ubuntu下默认安装了gcc,不过缺少必要的Header file,可以在命令行中输入下面的指令安装build-essential套件:sudo apt ...

  7. Java Integer常量池

    public class IntegerExample { public static void main(String[] javalatte) { Integer i = 10; Integer ...

  8. java基础---->多线程之Daemon(五)

    在java线程中有两种线程,一种是用户线程,另一种是守护线程.守护线程是一种特殊的线程,当进程中不存在非守护线程了,则守护线程自动销毁.今天我们通过实例来学习一下java中关于守护线程的知识.我是个平 ...

  9. jQuery回溯

    1.jQuery有个很好用的方法是 end(); 2.在进行链式操作时,使用end方法,可以回溯到上一个jQuery对象. 3.实现原理: jQuery内部有一个对象栈,当形成新的对象时,会将新对象推 ...

  10. Android SharedPreferences保存第一次的信息

    private void setHomeTimeZone() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPrefere ...