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 ...
随机推荐
- Spring MVC Rest服务 返回json报406错误的解决办法
@ResponseBody & @RequestBody @RequestBody 将 HTTP 请求正文插入方法中,使用适合的HttpMessageConverter将请求体写入某个对象. ...
- centos各版本信息
CentOS version Architectures RHEL base Kernel CentOS release date RHEL release date Delay (days) 2.1 ...
- jq变态全选vs原生变态全选
<script> $(function(){ var num=0; $("#btn").on('click',function(){ if(this.checked){ ...
- jq点击小图 弹出大图(更新版)
$(function(){ $(".fj1-consult").on("click",function(){ //设置弹框中图片的路径 $(".lay ...
- 几款极好的 JavaScript 文件上传插件
文件上传功能作为网页重要的组成部分,几乎无处不在,从简单的单个文件上传到复杂的批量上传.拖放上传,需要开发者花费大量的时间和精力去处理,以期实现好用的上传功能.这篇文章向大家推荐几款很棒的 JavaS ...
- 【Duke-Image】Week_3 Spatial processing
Chapter_3 Intensity Transsformations and Spatial Filtering 灰度变换与空间滤波 Intensity transformation functi ...
- session和cookie
第一次听到cookie这个词的时候着实兴奋了一段时间,以为是小饼干呢~快喝一杯82年的java压压惊!哈哈~ 与cookie的第一次邂逅——清缓存和清cookie 刚毕业的时候上班,做二次开发,明明后 ...
- 如何用JS判断推广链接所属的客服
今天有一个客户提出一个需求:网站有多个在线客服,每个客服都有自己的网站推广链接,当访客通过该客服的推广链接进入网站时,必须指定由该客服接待. 我的实现思路是获取推广链接中特定字符,然后判断字符对应的客 ...
- javascript实例备忘
一,javascript动态显示: 如显示效果上图所示: 如图显示鼠标放在百度谷歌等字样上市动态显示其内容明细:代码如下:<head><title></title> ...
- vue的transition过渡效果
需要4个类: *-enter: 进入的开始状态, *-enter-active: 进入的结束状态, *-leave: 离开的开始状态, *-leave-active: 离开的结束状态 vue-rout ...