描述

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. JUnit单元测试中的setUpBeforeClass()、tearDownAfterClass()、setUp()、tearDown()方法小结

    编写JUnit单元测试的时候,会用到 setUpBeforeClass().tearDownAfterClass().setUp().tearDown()这四个方法,例如用 eclipse新建一个ju ...

  2. Convolutional Patch Networks with Spatial Prior for Road Detection and Urban Scene Understanding

    Convolutional Patch Networks with Spatial Prior for Road Detection and Urban Scene Understanding 深度学 ...

  3. zabbix web monitoring 监控网页

    配置 Web 场景 配置 web 场景: 转到: 配置 (Configuration)–>主机 (或者 模板 ) 点击主机 (host)/ 模板 (template) 行中的 Web 点击右上角 ...

  4. window.location网页URL信息

    window.location属性 描述 hash 设置或获取 href 属性中在井号“#”后面的分段. host 设置或获取 location 或 URL 的 hostname 和 port 号码. ...

  5. 12个高效的VS调试技巧

    介绍 调试是软件开发周期中的一个很重要的部分,有时很有挑战性,有时候则让程序员迷惑,有时候让程序员发疯,但是.可以肯定的是,对于任何不是太那个微不足道的程序来说,调试是不可避免的.近年来,调试工具的发 ...

  6. float数据在内存中的存储方法

    浮点型变量在计算机内存中占用4字节(Byte),即32-bit.遵循IEEE-754格式标准.一个浮点数由2部分组成:底数m 和 指数e.                          ±mant ...

  7. 使用Gitolite搭建Gitserver

    Gitolite是一款Perl语言开发的Git服务管理工具.通过公钥对用户进行认证.并可以通过配置文件对些操作进行基于分支和路径的精细控制. Gitolite採用的是SSH协议而且使用SSH公钥认证. ...

  8. Atitit.ioc 动态配置文件guice 设计原理

    Atitit.ioc 动态配置文件guice 设计原理 1. Bat启动时注入配置文件1 2. ioc调用1 3. Ioc 分发器 配合 apche  MethodUtils.invokeStatic ...

  9. Android控件ListView获取item中EditText值

    能够明白,如今没有直接方法能够获得ListView中每一行EditText的值. 解决方式:重写BaseAdapter,然后自行获取ListView中每行输入的EditText值. 大概算法:重写Ba ...

  10. 10 Memcached 一致性哈希分布式算法原理与实现[PHP实现]

    <?php header("Content-type:text/html;charset=utf-8"); interface hash{ public function _ ...