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. 015 WAN

    Router#config t Enter configuration commands, one per line.  End with CNTL/Z. Router(config)#int s0/ ...

  2. C++ - 库函数优先级队列(priority_queue)输出最小值 代码

    库函数优先级队列(priority_queue)输出最小值 代码 本文地址: http://blog.csdn.net/caroline_wendy 库函数优先级队列(priority_queue)的 ...

  3. 连接App.config

    ConfigurationManager.AppSettings["AdminName"]; 连接App.config的字符

  4. Photoshop制作的ico图标方法

    photoshop是打不开ico的.只是能够通过安装插件实现. 插件点击这里能够下载. 用法,解压后的插件文件 粘贴到:  (英文版路径) /program files/adobe/photoshop ...

  5. COCOS学习笔记--即时动作ActionInstant

    Cocos引擎中的动作类的关系图例如以下: 能够看出,Action是继承自Ref类的,之前我的博客中也有讲过,Ref类是cocos2dx全部类的基类.动作类(Action)是全部动作的基类.它通过cr ...

  6. 十分简便的APK反编译(Mac 版本号 具体解释)

    之前參考了网上大神们介绍的apk for mac  的反编译的文章,里面写的十分具体而有用,可是因为apk for mac中反编译细节十分繁琐,过程也相对照较复杂,针对这个缺陷本人对其反编译的过程进行 ...

  7. unity3D游戏开发实战原创视频讲座系列13之帽子戏法游戏开发(预告)

    文件夹 第一讲  游戏演示项目创建 第二讲 游戏场景的编辑 第三讲  帽子的移动 第四讲  炮弹的产生 第六讲  游戏界面的完好 第七讲 各种UI的制作 第八讲  分数和爆炸特效   视持续更新中.. ...

  8. struts2开发中一些概念的理解

    对象关系映射(orm)中的两个概念 VO 和 PO: 它们都包含一些属性及这些属性的get/set方法 1.VO:是值对象,可以理解为业务对象,存活在业务层,供业务逻辑使用,当前业务逻辑需要一组什么数 ...

  9. luogu1314 聪明的质检员

    题目大意 小 T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有 n 个矿石,从 1 到 n 逐一编号,每个矿石都有自己的重量 wi 以及价值 vi.检验矿产的流程是: 1.给定 m 个区 ...

  10. [整理]EABI和OABI【转】

    本文转载自:https://www.crifan.com/order_eabi_and_oabi/ 1.什么是ABIABI,application binary interface (ABI),应用程 ...