377. Combination Sum IV
问题
Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.
Example:
nums = [1, 2, 3]
target = 4 The possible combination ways are:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1) Note that different sequences are counted as different combinations. Therefore the output is 7. 分析
根据题意,子问题可表示为 F(i) = F(i) + F(i - nums[j]),其中 i从1开始到target,j为数组下标,从0到nums.size()。通过计算可得,target为[1,target]的所有最大组合数,计算每个target用的是穷举遍历每个nums元素。 代码
int combinationSum4(vector<int>& nums, int target) {
vector<int> V(target + ,);
int i,j; V[] = ; //V[0]为1是因为i-nums[j] = 0 时 V[i-nums[j]] 成功一次
for(i = ;i <= target; i++)
{
for( j = ; j < nums.size(); j++)
if(nums[j] <= i)
V[i] += V[i - nums[j]];
} return V[target];
}
377. Combination Sum IV的更多相关文章
- LC 377. Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV
▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...
- [LeetCode] 377. Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- Leetcode 377. Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- 377. Combination Sum IV——DP本质:针对结果的迭代,dp[ans] <= dp[ans-i] & dp[i] 找三者关系 思考问题的维度+1,除了数据集迭代还有考虑结果
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- 377. Combination Sum IV 返回符合目标和的组数
[抄题]: Given an integer array with all positive numbers and no duplicates, find the number of possibl ...
- 377. Combination Sum IV 70. Climbing Stairs
back function (return number) remember the structure class Solution { int res = 0; //List<List< ...
- 377 Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
随机推荐
- 基于Swiper 2.7.6实现的带缩略图功能的轮播图
非原创,只能适合PC端,如果是移动端,只需要修改界面的大小即可.界面如下: 链接:http://pan.baidu.com/s/1pK9XdUV 密码:jsyk
- linux安装tomcat
一.下载tomcat 最新地址在:http://tomcat.apache.org 我下载的是:apache-tomcat-7.0.73.tar.gz,是已经编译好的包 解压文件后,移动到" ...
- thinkphp 3.2.3整合ueditor 1.4,给上传的图片加水印
今天分享一下thinkphp 3.2.3整合ueditor 1.4,给上传的图片加水印.博主是新手,在这里卡住了很久(>_<) thinkphp 3.2.3整合ueditor 1.4 下载 ...
- 如何在MyEclipse上添加更换JRE
如何在myeclipse上添加更换JRE 由于兼容性的问题,有些WEB项目会依赖jdk的版本.如果需要更换jdk,那么,知道如何更换JRE的方法很有必要. 一种在myeclipse上添加和更换JRE的 ...
- Excel—如何解决数组求和运算时行列不匹配产生的错误
1.如下所示: 使用SUM对两个数组A1:B2,C1:E3进行运算时,由于行列不匹配返回了错误值. 2.可通过使用IFERROR进行解决. 对判断生成的错误值,通过用0替代进行解决. 3.除以上外,由 ...
- Libero 使用拾忆
使用Libero软件进行管脚分配的时候可以使用脚本语言,详细的使用说明见des_constraints_ug.pdf(在Libero安装目录下寻找) 如: set_io srame_oe -REGIS ...
- MySQL报错:Got error 28 from storage engine
今天碰到数据库出错: Got error 28 from storage engine 查了一下,数据库文件所在的盘应该没事,应该是数据库用的临时目录空间不够 问题原因: 磁盘临时空间不够导致. 解决 ...
- C和指针 第十五章 错误报告perror和exit
15.1 错误报告 perror 任何一种程序都存在出错的可能,包括系统的函数库,当出现错误时,系统提示发生错误,标准库函数在一个外部整型变量中保存错误代码,然后把错误代码传给用户程序,提示错误原因. ...
- centos6.5 mysql-server 5.1.73启动失败
yum install mysql-server 安装mysql服务端会把相应的客户端也装上 service mysqld start 启动mysql服务 解决办法: 1.chomod 777 / ...
- runtime第二部分成员变量和属性
接上一篇 http://www.cnblogs.com/ddavidXu/p/5912306.html 转载来源http://www.jianshu.com/p/6b905584f536 http:/ ...