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?

Credits:
Special thanks to @pbrother for adding this problem and creating all test cases.

public class Solution {
public int combinationSum4(int[] nums, int target) {
if (nums.length==0) return 0;
Arrays.sort(nums); int[] combines = new int[target+1];
combines[0] = 1; for (int val=1;val<=target;val++)
for (int i=0;i<nums.length;i++)
if (val >= nums[i]){
combines[val] += combines[val-nums[i]];
} else break; return combines[target];
}
}

LeetCode-Combination Sum IV的更多相关文章

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

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

  2. Leetcode: Combination Sum IV && Summary: The Key to Solve DP

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

  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] Combination Sum 组合之和

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

  5. LeetCode Combination Sum III

    原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...

  6. Combination Sum | & || & ||| & IV

    Combination Sum | Given a set of candidate numbers (C) and a target number (T), find all unique comb ...

  7. LC 377. Combination Sum IV

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

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

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

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

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

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

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

随机推荐

  1. Cadence技巧01:利用Excel速新建原理图元件库

    Cadence技巧01:利用Excel速新建原理图元件库 听语音 | 浏览:1698 | 更新:2015-07-02 09:41 | 标签:excel 1 2 3 4 5 6 7 分步阅读 一键约师傅 ...

  2. 04、数据绑定控件 ListBox 的一个 Bug

    同事这两天在做 universal 项目的时候,遇到一个诡异的问题,即使设置 Page 为 缓存状态, 在页面跳转后, ListBox 的位置不会被缓存,怀疑是页面的缓存状态出了问题: this.Na ...

  3. Nginx设置expires设定页面缓存时间 不缓存或一直使用缓存

    配置expires expires起到控制页面缓存的作用,合理的配置expires可以减少很多服务器的请求 要配置expires,可以在http段中或者server段中或者location段中加入 l ...

  4. jquery的liveQuery插件

    一.livequery插件简介 jQuery的事件绑定功能使得jQuery代码与HTML代码能够完全分离,这样代码的层次关系更加清晰,维护起来也更加简单.然而对于动态加载到页面的HTML元素,每次都需 ...

  5. Jmeter测试带加密参数的接口

    在做接口测试时,很多时候我们都会碰到带有加密参数的接口,这种接口一般来讲都会有统一的加密方法,找开发要就好,Jmeter怎么去测呢 1.整体结构如下所示: 2.操作步骤 (1)将加密方法打成jar包放 ...

  6. C++重载IO操作符

    操作符的重载有一定的规则,而IO操作符必须重载为普通函数,且应该声明为类的友元函数.我试了,非友元也可以,但是必须提供访问成员变量的函数,所以,出于效率的考虑还是应该定义为友元. 规则如下: 1.  ...

  7. Python 内置模块函数filter reduce

    1.filter()实现过滤的功能 2.reduce()对序列中元素的连续操作可以通过循环来处理 3.map()对tuple元组进行解包操作,调用时设置map()的第一个参数为None 4.使用red ...

  8. vector--C++ STL 学习

    vector对应的数据结构为数组,而且是动态数组,也就是说我们不必关心该数组事先定义的容量是多少,它的大小会动态增长.与数组类似的是,我们可以在末尾进行元素的添加和删除,也可以进行元素值的随机访问和修 ...

  9. ES-PHP向ES批量添加文档报No alive nodes found in your cluster

    ES-PHP向ES批量添加文档报No alive nodes found in your cluster 2016年12月14日 12:31:40 阅读数:2668 参考文章phpcurl 请求Chu ...

  10. 115个Java面试题和答案(上)

    转自:http://www.importnew.com/10980.html 本文我们将要讨论Java面试中的各种不同类型的面试题,它们可以让雇主测试应聘者的Java和通用的面向对象编程的能力.下面的 ...