描述

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. 【VBA】查看当前窗口的宽与高

    打开的Excle如何使用VBA查看当前窗口的宽与高呢?代码如下: Sub 查看宽度与高度() Dim myWidth As Double Dim myHeigth As Double myWidth ...

  2. 会话管理之Cookie技术

    会话管理是web开发中比较重要的环节,这一节主要总结下会话管理中的cookie技术. 1. 何为会话 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个 ...

  3. 多trac的安装和配置

    其他相关网页: trac+svn: http://wenku.baidu.com/view/84389a81ec3a87c24028c43f.html apache(GCI):http://hi.ba ...

  4. JAVA读取文件夹大小的几种方式

    (一)单线程递归方式 package com.taobao.test; import java.io.File; public class TotalFileSizeSequential { publ ...

  5. eclipse JVM Tomcat 内存堆栈大小设置

    1,  设置Eclipse内存使用情况 修改eclipse根目录下的eclipse.ini文件 -vmargs  //虚拟机设置 -Xms40m //初始内存 -Xmx256m //最大内存 -Xmn ...

  6. FastDFS分布文件系统

    FastDFS是为互联网应用量身定做的一套分布式文件存储系统,非常适合用来存储用户图片.视频.文档等文件.对于互联网应用,和其他分布式文件系统相比,优势非常明显.具体情况大家可以看相关的介绍文档,包括 ...

  7. 小技巧:怎样以另外的Windows用户执行SSMS

    可能会碰到这种问题.你须要在一台机器上面使用不同的Windows账户连接到SQL Server做測试.默认情况下,你须要用不同的Windows账户登录然后測试. 实际上不须要每一个windows登陆. ...

  8. python设置环境变量

    方法1: 当程序开发完毕,就可以添加到 '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages'中 ...

  9. web安全之SQL注入--第一章 课程介绍

    课程介绍1.什么是SQL注入?2.如何寻找SQL注入漏洞?3.如何进行sql注入攻击?4.如何预防sql注入5.课程总结

  10. kafka的并行度与JStorm性能优化

    kafka的并行度与JStorm性能优化 > Consumers Messaging traditionally has two models: queuing and publish-subs ...