描述

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]
]

思路一:递归

定义一个临时vector,然后利用递归的方法从前到后遍历所有元素,已经遍历过的就跳过,就这样循环遍历

Runtime: 8 ms

class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> res;
if(candidates.size() == ) return res;
sort(candidates.begin(), candidates.end());
vector<int> tmp;
combinationSum(candidates, target, res, tmp, );
return res;
} void combinationSum(vector<int>& candidates, int target, vector<vector<int>>& res, vector<int>& tmp, int begin){
if(!target){
res.push_back(tmp); //如果target在等于0的时候添加到res,只有target等于0时进入
return;
}
for(int i = begin; i != candidates.size() && target - candidates[i] >= ; ++i){
tmp.push_back(candidates[i]); //加入到tmp中
combinationSum(candidates, target - candidates[i], res, tmp, i); //遍历下一个元素,进入递归
tmp.pop_back(); //跳过上次遍历,开始输入后续元素,直到tmp为空,向后移位继续遍历
}
}
};

思路二:动态规划

DP[0] = [[ ]],

DP[j] = DP[j] + (DP[j - score] + tmp)

在j位置的DP元素根据现在的score与j-score位置的数组,每一个相加得到

这样慢慢补充DP[j],直到得到正确答案

运行时间:12 ms

class Solution {
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
sort(candidates.begin(), candidates.end()); //先对数组进行排序
vector< vector< vector<int> > > DP(target + , vector<vector<int>>()); //初始化DP数组,长度为target + 1
DP[].push_back(vector<int>()); // 初始化DP[0]为[[]]
for (auto& score : candidates) // 开始遍历给定的数组
for (int j = score; j <= target; j++){ //从DP的j~target遍历
auto tmp = DP[j - score]; //找到tmp位置使得tmp + score = j
if (tmp.size() > ) {
for (auto& s : tmp)
s.push_back(score); //将score添加到tmp的每一个元组里
DP[j].insert(DP[j].end(), tmp.begin(), tmp.end()); //在DP[j]的末尾加入tmp
}
}
return DP[target];
}
};

【LeetCode】 数相加组合 Combination Sum的更多相关文章

  1. Leetcode 39 40 216 Combination Sum I II III

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

  2. LeetCode(40) Combination Sum II

    题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...

  3. leetcode第38题--Combination Sum

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

  4. LeetCode笔记:39. Combination Sum

    题目描述 给定一个无重复的正整数数组 candidates 和一个正整数 target, 求所有和为 target 的 candidates 中数的组合中.其中相同数的不同顺序组合算做同一种组合,ca ...

  5. 数字组合 · Combination Sum

    不能重复: [抄题]: 给出一个候选数字的set(C)和目标数字(T),找到C中所有的组合,使找出的数字和为T.C中的数字可以无限制重复被选取. 例如,给出候选数组[2,3,6,7]和目标数字7,所求 ...

  6. leetcode第39题--Combination Sum II

    题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...

  7. LeetCode(39) Combination Sum

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

  8. leetcode个人题解——#39 Combination Sum

    思路:先对数据进行排序(看评论给的测试数据好像都是有序数组了,但题目里没有给出这个条件),然后回溯加剪枝即可. class Solution { public: ; vector<vector& ...

  9. Leetcode 之 Combination Sum系列

    39. Combination Sum 1.Problem Find all possible combinations of k numbers that add up to a number n, ...

随机推荐

  1. Loadrunner中对中文进行UTF-8转码的探索

    上一篇 / 下一篇  2010-02-22 15:20:28 查看( 2378 ) / 评论( 2 ) / 评分( 5 / 0 ) 这是一个HTTP接口测试中经常会碰到的问题,目前的服务器采用的都是U ...

  2. 安装pydot及importError解决办法

    安装pydot: 需要先安装graphviz和pyparsing. 安装pydot过程曾出现“import error” 错误,主要是版本不兼容的问题.之后自己apt-get upgrade了一下,问 ...

  3. C#开发--FTP操作方法管理

    1.整理简化了下C#的ftp操作,方便使用    1.支持创建多级目录    2.批量删除    3.整个目录上传    4.整个目录删除    5.整个目录下载 2.调用方法展示, var ftp ...

  4. Android下的HttpClient的使用(9.11)

    1 http://liangruijun.blog.51cto.com/3061169/803097   备注:此博客较早,android 4.0之后不允许在UI线程进行网络操作,所以没有输出结果. ...

  5. SQL Server中LIKE和PATINDEX的用法

    在SQL Server中,能使用通配符的只有2个:LIKE.PATINDEX. 不过LIKE支持2种通配符转义,无限制最全面:而PATINDEX只支持最简单的通配符转义([]转义),限制较多. LIK ...

  6. 自己动手开发IOC容器

    前两天写简历.写了一句:精通Spring IoC容器.怎么个精通法?还是自己动手写个IOC容器吧. 什么是IoC(Inversion of Control)?什么是DI(Dependency Inje ...

  7. Atitit.jdk java8的语法特性详解 attilax 总结

    Atitit.jdk java8的语法特性详解 attilax 总结 1.1. 类型推断这个特别有趣的.鲜为人知的特性1 2. Lambda1 2.1. 内部迭代意味着改由Java类库来进行迭代,而不 ...

  8. APICloud打包Vue单页应用

    APICloud新建项目后,会生成以下目录结构 其中index.html是入口文件,而vue-cli打包生成的文件是在dist目录下 ├─dist│ └─static│ ├─css│ └─js │ └ ...

  9. cat 命令

    cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. 1.命令格式: cat [选项] [文件] ...

  10. Swing与javafx直接调用

    Swing调用javafx 调用方法: Platform.runLater(new Runnable(){ @Override public void run() { WebView webView ...