描述

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. httpModules 与 httpHandlers

    ASP.NET对请求处理的过程:当请求一个*.aspx文件的时候,这个请求会被inetinfo.exe进程截获,它判断文件的后缀(aspx)之后,将这个请求转交给ASPNET_ISAPI.dll,AS ...

  2. 未能从程序集“Oracle.ManagedDataAccess”加载 “OracleInternal.Common.ConfigBaseClass”

    使用VS2015做项目的过程中一直使用的服务器上的oracle数据库,后来想学习一下oracle,就在本机安装了oracle.可没想到本来运行好好的项目,现在不能运行了.项目是使用的Abp框架,当运行 ...

  3. WordPress函数:get_bloginfo()用法详解

    描述 返回你博客的信息,这些信息可以用在任何地方的 PHP 代码中.这个函数,和 bloginfo() 一样,可以用来在模板文件的任何地方显示你博客的信息. 用法 <?php $bloginfo ...

  4. 动态添加ImageView 设置setPadding不起作用问题

    imageView = new ImageView(NavigationActivity.this); imageView.setLayoutParams(new LayoutParams(12,12 ...

  5. samba基本配置

    安装启动不用说了 vim /etc/samba/smb.conf workgroup = WORKGROUP server string = Samba Server %vnetbios name = ...

  6. Maven的pom文件内容详细理解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  7. Linux_经常使用命令

    1. ls显示文件夹文件夹及文件使用方式: ls -lt -a 显示文件夹下全部文件及文件夹包括 . 与 .. -A 显示文件夹下全部文件及文件夹不包括 . 与 .. -l 显示文件夹下全部文件及文件 ...

  8. vmware workstation导出ovf

    ovf tool路径 /Applications/VMware Fusion.app/Contents/Library/VMware OVF Tool 上面红色的部分,需要右键点击应用程序中的VMwa ...

  9. struts2中配置全局日期类型转换器

    1.编写一个类,继承StrutsTypeConverter,实现其中的convertFromString和convertToString方法,该类如下: package me.edu.utils; i ...

  10. 走进科学之揭开神秘的"零拷贝"!

        "零拷贝"这三个字,想必大家多多少少都有听过吧,这个技术在各种开源组件中都使用了,比如kafka,rocketmq,netty,nginx等等开源框架都在其中引用了这项技术 ...