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?

例子:[1,2,3,5,6], target = 8。dp[8] 意为有多少种方式可以生成8。

例如: 8=1(in nums) + 7 = 2(in nums) + 5 = 3(in nums) + 4= 5(in nums) + 3 = 6(in nums) + 2。 所以dp[8] = dp[7] + dp[5] + dp[4] + dp[3] + dp[2]。

5=5(in nums) + 0 = 3(in nums) + 2 = 2(in nums) + 3 = 1(in nums) +4。 所以dp[5] = dp[0] + dp[2] + dp[4] + dp[4]。

 class Solution(object):
def combinationSum4(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
dp = [0]*(target + 1)
dp[0] = 1
for i in range(target+1):
for n in nums:
if i + n <= target:
dp[i+n] += dp[i]
return dp[-1]

Leetcode 377. Combination Sum 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] 377. Combination Sum IV 组合之和之四

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

  3. LC 377. Combination Sum IV

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

  4. 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV

    ▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...

  5. 【LeetCode】377. Combination Sum IV 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 377 Combination Sum IV 组合之和 IV

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

  7. 377. Combination Sum IV

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

  8. leetcode日记 Combination sum IV

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

  9. 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 ...

随机推荐

  1. .net 已知图片的网络路径,通过浏览器下载图片

    没什么技术含量,主要留给自己查找方便: 如题,知道图片的完整网络路径的情况下,在浏览器中下载图片的实现: 下面这个方法实现的是把图片读取为byte数组: private byte[] GetImage ...

  2. Ant :DataType

    DataType patternset fileset selector filelist path regexp Ant datatype Ant中,除了Property可以做为Task执行时使用的 ...

  3. .NET/ASP.NETMVC 深入剖析 Model元数据、HtmlHelper、自定义模板、模板的装饰者模式(三)

    阅读目录: 7.HtmlHelper.HtmlHelper<T>中的ViewModel的类型推断 8.控制ViewModel中的某个属性的呈现(使用PartialView部分视图细粒度控制 ...

  4. 【hbase】——bulk load导入数据时value=\x00\x00\x00\x01问题解析

    一.存入数据类型 Hbase里面,rowkey是按照字典序进行排序.存储的value值,当用filter进行数据筛选的时候,所用的比较算法也是字典序的. 1.当存储的value值是float类型的时候 ...

  5. Oracle 收缩数据文件

    Oracle数据库中当某些大数据表不再需要的时候,我们可以通过以下方式来进行清理: DROP TABLE TBL_TEST PURGE; 但是对应表空间(tablespace)中的磁盘空间并没有得到释 ...

  6. Linux 之 GCC 和 GDB

    1  GCC = GNU C Compiler 1)  pre-processing (预处理) $ gcc -E -o hello.i hello.c // -E 预处理后停止 2)  compil ...

  7. UVA11021 Tribles[离散概率 DP]

    UVA - 11021 Tribles GRAVITATION, n. “The tendency of all bodies to approach one another with a stren ...

  8. CodeForces 485C Bits[贪心 二进制]

    C. Bits time limit per test1 second memory limit per test256 megabytes inputstandard input outputsta ...

  9. hibernate 数据处理

    1.Hibernate的配置文件 Hibernate.cfg.xml Dept.hbm.xml 2.CRUD的方法名称和参数类型 Save(Object obj) Delete(Object obj) ...

  10. 使用spring方式来实现aop编程

    1:什么是aop? Aspect Oriented Programming 面向切面编程 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译 ...