leetcode日记 Combination sum IV
题目:
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的更多相关文章
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] 377. Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- Leetcode 377. Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [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 ...
- Leetcode 之 Combination Sum系列
39. Combination Sum 1.Problem Find all possible combinations of k numbers that add up to a number n, ...
- [LeetCode] 39. Combination Sum 组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- 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 ...
- Combination Sum | & || & ||| & IV
Combination Sum | Given a set of candidate numbers (C) and a target number (T), find all unique comb ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
随机推荐
- Google C++ Style Guide
Background C++ is one of the main development languages used by many of Google's open-source project ...
- PR曲线,ROC曲线,AUC指标等,Accuracy vs Precision
作为机器学习重要的评价指标,标题中的三个内容,在下面读书笔记里面都有讲: http://www.cnblogs.com/charlesblc/p/6188562.html 但是讲的不细,不太懂.今天又 ...
- mysql delete 使用别名 语法
今天删除数据,写了这么条sql语句, DELETE from sys_menus s WHERE s.MENU_ID in (86,87,88); 结果报错.. [Err] 1064 - You ...
- 完美解释if-modified-since/not-modified 文件头的意义
http://www.cnblogs.com/zh2000g/archive/2010/03/22/1692002.html 很好很强大
- centos5.11 repo 安装mysql5.7
http://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html mysql yum repo 安装说明 http://d ...
- 用到的一些python包,记录下
Requests beautifulsoup lxml logging gevent django Bottle numpy pandas sklearn pyopencv opencv_python ...
- oracle 存储过程创建及执行简单实例
1. 创建 CREATE OR REPLACE PROCEDURE getAplage(eNo IN NUMBER,salary OUT NUMBER) AS BEGIN SELECT AplAge ...
- 【转】PCI学习笔记
1.PCI设备编号 每一个PCI device都有其unique PFA(PCI Fcntion Address) PFA由 bus number.device number.functi ...
- Gradle 使用本地的Jar包(gradle oracle ojdbc14 )
Gradle 使用本地的Jar包(gradle oracle ojdbc14 ) 因为Oracle的驱动包在Maven上是没办法直接下载到的,所以在使用Gradle的使用,会导致无法加载Oracle, ...
- Unity3D 中的3种坐标系
Unity3D Script API : Camera 若干文章: 1.Screen VS Viewport What is the difference 2.Screen,Viewport有什麽區別 ...