题目:

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.

一开始最直接的思路就是递归:拿给定的例子来说就是nums=[1,2,3] target=4 相当于,那么遍历nums,每次递归调用时将target-nums[i]作为参数传入下一层,这样就可以找到所有的组合,然后返回结果的个数即可。但是这样的尝试却TLE了,分析发现确实如此,即使nums中数目不多,只要target一大,那么递归的层数就会过多,这样一来极大的增加了时间复杂度。

随后便再想是不是能用DP来进行求解,从这个角度来看的话,这个问题就特别想斐波那契数列的DP求法,题目要求求得是不同排列的数目,那么很容易会发现,每一个数所得的排列数目都和之前的排列数目有关,和刚才的递归想法一致,只不过我们关注的是排列的数目而不是排列本身,target=4所得的排列数目按照[1,2,3]就可以分解为target=3,target=2,target=1的数目之和。如此一来,只要了解了nums就可以一步一步计算出target的排列数目。

java代码如下:

public int combinationSum4(int[] nums, int target) {
int result[]=new int[target+1];
result[0]=1;//如果target是nums中的一员,那么nums[0]就可以来表示这个数本身就可以当做一个排列
for (int i=1;i<target+1;i++)
for (int j=0;j<nums.length;j++){
if(i-nums[j]<0)
continue;
result[i]+=result[i-nums[j]];
}
return result[target];
}

python代码如下:

def combinationSum4(self, nums, target):

        """
:type nums: List[int]
:type target: int
:rtype: int
"""
result=[0]*(target+1)
result[0]=1
for i in xrange(0,target+1):
for j in xrange(0,len(nums)):
if i-nums[j]<0:
continue
result[i]+=result[i-nums[j]]
return result[target]

最后附上TLE 递归方法:(java)

public class CombinationSumIV {

    //递归复杂度太高,须其他方法
private static int count=0;
public int combinationSum4(int[] nums, int target) {
if (nums.length==0){
return 0;
}
ArrayList<Integer> temresult=new ArrayList<>();
for(int i:nums){
if (i > target){
continue;
}
else{
temresult.add(i);
deep(nums,target-i,temresult);
}
}
return count;
}
private void deep(int[] nums,int target,ArrayList<Integer> temresult){
if (target==0){
count++;
}
else {
for (int i:nums){
if (i>target){
continue;
}
else{
temresult.add(i);
deep(nums,target-i,temresult);
}
}
}
}
}

leetcode日记 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. Leetcode 377. Combination Sum IV

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

  4. [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 ...

  5. Leetcode 之 Combination Sum系列

    39. Combination Sum 1.Problem Find all possible combinations of k numbers that add up to a number n, ...

  6. [LeetCode] 39. Combination Sum 组合之和

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  7. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  8. Combination Sum | & || & ||| & IV

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

  9. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

随机推荐

  1. java 执行jar指定log4j.properties文件位置

    默认情况下,log4j.properties会被加载,并且这个文件需要在classpath根目录,当打包jar时,会打包打jar内部,当需要修改日志级别时,会比较麻烦 可以使用:java -jar - ...

  2. 不再为Apache进程淤积、耗尽内存而困扰((转))

    本篇文章是为使用Apache+MySQL,并为Apache耗尽内存而困扰的系统管理员而写.如果您没有耐心读完本文,请参考以下步骤: 修改/etc/my.cnf,加上这样一行: log-slow-que ...

  3. HackPwn2015:IoT智能硬件安全威胁分析

    HackPwn2015:IoT智能硬件安全威胁分析 360安全卫士 · 2015/08/26 14:43 IoT(物联网)是一种既危险又有趣的混合技术,所谓的混合技术包括移动应用程序.蓝牙.Wi-Fi ...

  4. 《C++ Primer》学习笔记【第二部分 C++标准库】

    第8章 IO库 IO对象不能复制,即1.IO对象不能存储在vector或其他容器中   2.如果需要传递或返回IO对象,必须传递或返回指向该对象的指针或引用. 一般情况下,如果要传递IO对象以便对它进 ...

  5. web前端图片极限优化策略

    随着web的发展,网站资源的流量也变得越来越大.据统计,60%的网站流量均来自网站图片,可见对图片合理优化可以大幅影响网站流量,减小带宽消耗和服务器压力. 一.现有web图片格式 我们先来看下现在常用 ...

  6. MFC编程入门之十八(对话框:字体对话框)

    在上一节为大家讲解了文件对话框的使用,本节则主要介绍字体对话框如何应用. 字体对话框的作用是用来选择字体.我们也经常能够见到.MFC使用CFontDialog类封装了字体对话框的所有操作.字体对话框也 ...

  7. 转!java基础笔记

    原博文地址:http://blog.csdn.net/u012152619/article/details/48024345 Java标识符 Java所有的组成部分都需要名字.类名.变量名以及方法名都 ...

  8. PPTP协议

    PPTP协议 PPTP(Point-to-Point Tunneling Protocol)点对点隧道协议是PPP协议的一种扩展,它将PPP帧封装进IP包中,通过IP网络进行传输.它通过PPTP控制连 ...

  9. sublime jsx 格式化工具

    1.到Sublime Text 3\Packages\ 目录下 git clone https://github.com/ionutvmi/sublime-jsfmt jsfmt 2.重启sublim ...

  10. js创建标签的方法--依赖于jquery

    /** * 创建标签,传入一个对象,返回一个完整的标签 * @param {Object.attribute} tag 标签 * @param {Object.attribute} attribute ...