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. ELK5.X使用X-Pack配置密码

    一.前言 前面使用ELK5.X+logback搭建日志平台,但是,当访问kibana 时,直接就可以访问了,如果设置登录名和密码,是不是更好呢?答案是肯定的,这里使用X-Pack来配置登录名和密码. ...

  2. 编写一个读写倾斜测量数据.s3c文件格式的OSG插件osgdb_s3c

    VS新建一个空的DLL工程 ReaderWriterS3C.cpp源文件 #include <osg/Notify> #include <osgDB/FileNameUtils> ...

  3. Qt监控后台服务运行状态

    mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QMa ...

  4. linux 启动时文件系统错误

    由于我把/dev/VolGroup00/lvData写在了/etc/fstab中,后面又把这个lv给删除了,导致出现下面开机启动的错误: 解决办法: 1.进入单用户模式下,当修改/etc/fstab时 ...

  5. PyQt4预定义对话框

    PyQt4中的对话框 对话窗口和对话框是现代GUI应用程序必不可少的一部分.生活中“对话”被定义为发生在两人或更多人之间的会话.而在计算机世界,“对话”则时人与应用程序之间的“会话”.人及对话的形式有 ...

  6. mysql concat

    CONCAT_WS() 代表 CONCAT With Separator ,是CONCAT()的特殊形式. 第一个参数是其它参数的分隔符.分隔符的位置放在要连接的两个字符串之间. 分隔符可以是一个字符 ...

  7. 优秀的PHP开发者是怎样炼成的?

    4.在数据库中避免使用联合操作 比起其它的Web编程语言来说,PHP的数据库功能十分强大.但是在PHP中数据库的运行仍然是一件十分费时费力的事情,所以,作为一个Web程序员,要尽量减少数据库的查询操作 ...

  8. 谈谈java中的构造函数

    from 本篇博文主要是为新手.对java语言感兴趣的人和那些没有系统学习过java基础知识的人进行一个总结,在文章中对构造函数进行了较为详细的说明和讨论,也包含了我个人对于java面向对象中构造函数 ...

  9. webpack之跨域

    前后端分离开发中,本地前端开发调用接口会有跨域问题,一般有以下几种解决方法: 直接启动服务端项目,再将项目中的资源url指向到前端服务中的静态资源地址,好处在于因为始终在服务端的环境中进行资源调试,不 ...

  10. linux的~和/的区别

    转自:https://zhidao.baidu.com/question/166486946.html /是目录层的分隔.表示符.只有一个/表明是root,/etc/表明是根目录下面的etc目录(当然 ...