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. git的常见操作方法

    GIT操作方法 http://git.oschina.net/ g进入https://git-for-windows.github.io/下载安装 g启动命令窗口输入以下内容 git config - ...

  2. Mysql 比较运算符详解

    熟悉了最简单的算术运算符,再来看一下比较运算符.当使用SELECT 语句进行查询时,MySQL允许用户对表达式的左边操作数和右边操作数进行比较,比较结果为真,则返回1,为假则返回0,比较结果不确定则返 ...

  3. 使用VS2012遇到的问题

    问题1:VS2012 编译程序时:无法查找或打开PDB文件 解决方法:调试-选项-符号-Microsoft符号服务器打钩,然后确定,就OK了. 问题2:按F5运行.c程序,dos窗口闪退 解决方法:C ...

  4. vector常见用法

    #include <boost/foreach.hpp> #include <iostream> #include <vector> #include <bo ...

  5. Openstack 03 - Nova Compute

    1.前言 非常早之前就開始着手写Openstack 系列的博客了,在写了总体架构和Keystone之后,准备写Nova,可是每次写到一半,自己心里就认为不踏实,由于似乎我并没有真正理解Nova,或者说 ...

  6. Selenium - Webdriver API /ActionChains API

    一.控制浏览器 1.1 控制浏览器窗口大小 # 获取当前浏览器的大小  driver.get_window_size() # 通过像素设置浏览器的大小  driver.set_window_size( ...

  7. 每日英语:China's Bad Earth

    In Dapu, a rain-drenched rural outpost in the heart of China's grain basket, a farmer grows crops th ...

  8. jmap 查看内存使用直方图

    jps   -- 查看进程号 jmap -histo pid  查看堆内存中的对象数目.大小统计直方图, 如果带上live则表示先进行一次fullgc 再统计内存使用情况,如下: jmap -hist ...

  9. xadmin 安装详解

    1.安装必要的包 django>=1.9.0 django-crispy-forms>=1.6.0 django-import-export>=0.5.1 django-revers ...

  10. 由于内部错误,服务器无法处理该请求。有关该错误的详细信息,请打开服务器上的 IncludeExceptionDetailInFaults (从 ServiceBehaviorAttribute 或从 <serviceDebug> 配置行为)以便将异常信息发送回客户端,或打开对每个 Microsoft .NET Framework SDK 文档的跟踪并检查服务器跟踪日志。

    客户端调用WCF的时候报上面的错误,WCF只能序列化基础的数据类型,不能直接序列化SqlParameter类型,需要使用自定义类,然后在WCF服务端转换的方式解决: 自定义类代码如下: using S ...