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. Ribbon 和 wowza 的集成开发

            前言        Ribbon 是提供 REST 服务的区域感知负载均衡器,它在 wowza 的前端,应该部署在专业的 REST 容器下,而不是流媒体服务器 wowza 下. 本文介 ...

  2. (原创)android6.0系统 PowerManager深入分析(很具体)

    概述 一直以来,电源管理是电子产品设计中很重要的环节.也是不论什么电子设备中最为重要的系统模块之中的一个,优秀的电源管理方案.可以提供持久的续航能力,良好的用户体验.更能提升电子产品的竞争力. 移动设 ...

  3. java工程资源文件夹嵌套建立!

    做过一遍忘了,找了半天!记这里,看你还忘不忘!

  4. 每日英语:Investing the Downward Dog Way? Adviser Suggests Deep Breaths

    When the Dow Jones Industrial Average hit a new record this past March, Brent Kessel awoke at 3:30 a ...

  5. excel鼠标拖选慢shift选择快的问题

    今天遇到个惊天大坑,关于excel的,最近,一直在调查这个东西,刚开始真的是毫无头绪,反正现在就是excel的值的copy会偶尔慢,慢的情况也是不明白,就是稀里糊涂的调查. 刚开始连100%再现这个b ...

  6. Qt 中彩色图像转换为灰度图

    近期在做几个图像处理相关的项目.里面有一个操作就是须要先将彩色图像转换为灰度图像. QImage 有一个convertToFormat方法.最開始一直用这个函数来实现. 可是今天细致看了看,发现这个函 ...

  7. SpringMVC请求后台地址URL没有.*的几种实现方式

    今天做项目,由于项目是通过扫二维码进入,二维码存放的地址不希望有 .do,而是http:xxxx:8080/xxx/yyy/zzz的格式(zzz为参数),但是项目其它请求url后面都必须要有.do,想 ...

  8. struts2 OGNL ValueStack概念理解 # % $ 的区别

    http://blog.csdn.net/tjujacob/article/details/8117659 —————————————————————————————————————————————— ...

  9. KMP + 求最小循环节 --- HUST 1010 - The Minimum Length

    The Minimum Length Problem's Link: http://acm.hust.edu.cn/problem/show/1010 Mean: 给你一个字符串,求这个字符串的最小循 ...

  10. ActionContextCleanUp

    ActionContextCleanUp作用   延长action中属性的生命周期,包括自定义属性,以便在jsp页面中进行访问,让actionContextcleanup过滤器来清除属性,不让acti ...