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.

Follow up:
What if negative numbers are allowed in the given array?
How does it change the problem?
What limitation we need to add to the question to allow negative numbers?

详见:https://leetcode.com/problems/combination-sum-iv/description/

C++:

class Solution {
public:
int combinationSum4(vector<int>& nums, int target) {
vector<int> dp(target+1);
dp[0]=1;
for(int i=1;i<=target;++i)
{
for(int num:nums)
{
if(i>=num)
{
dp[i]+=dp[i-num];
}
}
}
return dp.back();
}
};

参考:https://www.cnblogs.com/grandyang/p/5705750.html

377 Combination Sum IV 组合之和 IV的更多相关文章

  1. [LeetCode] 377. Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  2. [LeetCode] 216. Combination Sum III 组合之和 III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. [LeetCode] Combination Sum III 组合之和之三

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  4. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  5. [LeetCode] 40. Combination Sum II 组合之和 II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  6. LeetCode OJ:Combination Sum II (组合之和 II)

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

  7. [LeetCode] Combination Sum II 组合之和之二

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

  8. [LeetCode] 40. Combination Sum II 组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  9. [Leetcode] combination sum ii 组合之和

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

随机推荐

  1. SOJ 4454 (矩阵快速幂)

    先引入数的快速幂 例如计算2的5次方,常规算法2*2*2*2*2,利用快速幂的思想,求出5的二进制表达式101,权值为1和4的位上数字为1,即2^5=2^1*2^4.代码如下,时间复杂度为O(logn ...

  2. P1614 爱与愁的心痛

    洛谷——P1614 爱与愁的心痛 题目背景 (本道题目隐藏了两首歌名,找找看哪~~~) <爱与愁的故事第一弹·heartache>第一章 <我为歌狂>当中伍思凯神曲<舞月 ...

  3. 所有的异常都要使用try catch 语句捕获?

    在开发应用程序过程中必须检测代码可能发生的错误并进行正确的处理,这个在理想的情况下,应用程序中的每行 代码都按照预想的执行,要用到的每种资源总是可以利用,但是在实际的开发过程中,写代码难免会出错,或是 ...

  4. Ubuntu清理内存命令(效果不明显)

    注意:最好不要在生产环境上使用!!! 1.检查内存使用情况 watch -n 3 free -m watch -n 3 cat /proc/meminfo 2.清理 #释放页缓存 echo 1 > ...

  5. Android GIS开发系列-- 入门季(11) Callout气泡的显示

    一.气泡的简单显示 首先我们要获取MapView中的气泡,通过MapView的getCallout()方法获取一个气泡.看一下Callout的简单介绍: 大体的意思是通过MapView获取Callou ...

  6. Windows 2008 R2 SP1部署WSUS 3.0 SP2

    1 实验环境 1)域: 域名为fengxja.com: 网段:192.168.0网段,不连接外网. 域功能级别和林功能级别为Windows server 2003模式. 2)DC服务器: 域控制器: ...

  7. node安装-Win+Linux+Mac osx

    node下载地址,除了Mac osx或Win平台,仅有Linux平台命令安装. Win.Mac 点击即可下载(注:Mac有dmg和pkg安装格式). Linux分为Redhot和Deepin系列,安装 ...

  8. 删除子节点XML数据

    XmlDocument xDoc = new XmlDocument(); xDoc.Load(txtValueHelper.txtValue); XmlNodeList list = xDoc.Se ...

  9. 在不同的系统中的virtualbox中安装Ubuntu SDK

    对非常多的开发人员来说.你们可能使用的不是Ubuntu操作系统.在这样的情况下,开发人员须要在自己的操作系统中(OS X及Windows)安装virtualbox,并在VirtualBox中安装Ubu ...

  10. HDU 5000 2014 ACM/ICPC Asia Regional Anshan Online DP

    Clone Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other) Total Submiss ...