题目:

关于动态规划类题目的思路如何找在上一篇博客 https://www.cnblogs.com/niuyourou/p/11964842.html 讲的非常清楚了,该博客也成为了了leetcode中戳气球题目点赞和阅读最多的题解(虽然题解本身就很少)。

本题的解题路径与上述博客一致,也是从 递归分治动态规划

各个解法之间的过渡不再赘述,有兴趣的朋友可以看看我的上述博客。https://www.cnblogs.com/niuyourou/p/11964842.html

这次我们只贴关键代码供各位参考:

递归搜索解法:

  /**
* @Author Nxy
* @Date 2019/12/21
* @Param
* @Return
* @Exception
* @Description 递归搜索
*/
int i = 0; public int combinationSum4(int[] nums, int target) {
if (nums == null) {
return 0;
}
combinationSum4(nums, 0, target);
return i;
} public void combinationSum4(int[] nums, int beforeRe, int target) {
if (beforeRe > target) {
return;
}
if (beforeRe == target) {
i++;
return;
}
int length = nums.length;
for (int i = 0; i < length; i++) {
int tempRe = beforeRe + nums[i];
combinationSum4(nums, tempRe, target);
}
}

分治解法:

状态转移方程:dp[i] = sum{ dp[i - num] for num in nums and if i >= num }

    /**
* @Author Nxy
* @Date 2019/12/21
* @Param
* @Return
* @Exception
* @Description 分治加缓存
*/
public int combinationSum4II(int[] nums, int target) {
if (nums == null) {
return 0;
}
int length = nums.length;
Map<Integer, Integer> cache = new HashMap<Integer, Integer>();
return combinationSum4II(nums, target, length, cache);
} public int combinationSum4II(int[] nums, int target, int length, Map<Integer, Integer> cache) {
if (target < 0) {
return 0;
}
if (target == 0) {
return 1;
}
Set s = cache.keySet();
if (s.contains(target)) {
return cache.get(target);
}
int temp = 0;
for (int i = 0; i < length; i++) {
temp += combinationSum4II(nums, target - nums[i], length, cache);
}
cache.put(target, temp);
return temp;
}

从递归到分治的效率提升:

 动态规划解法:

/**
* @Author Nxy
* @Date 2019/12/21
* @Param
* @Return
* @Exception
* @Description DP解法
*/
public int combinationSum4III(int[] nums, int target){
if(nums==null){return 0;}
int length=nums.length;
int[] cache=new int[target+1];
cache[0]=1;
for(int i=1;i<=target;i++){
int temp=0;
for(int j=0;j<length;j++){
if(i-nums[j]==0){
temp++;
continue;
}
if(i-nums[j]>0){
temp+=cache[i-nums[j]];
}
}
cache[i]=temp;
}
return cache[target];
}

效率提升:

递归太费时,我们单独看下分治到动态规划的效率提升:

leetcode组合总和 Ⅳ 解题路径的更多相关文章

  1. 图解Leetcode组合总和系列——回溯(剪枝优化)+动态规划

    Leetcode组合总和系列--回溯(剪枝优化)+动态规划 组合总和 I 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 ...

  2. 34,Leetcode 组合总和I,II -C++ 回溯法

    I 题目描述 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的数字可以无 ...

  3. LeetCode 组合总和(dfs)

    题目 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重 ...

  4. Leetcode题目39.组合总和(回溯+剪枝-中等)

    题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无 ...

  5. Leetcode 377.组合总和IV

    组合总和IV 给定一个由正整数组成且不存在重复数字的数组,找出和为给定目标正整数的组合的个数. 示例: nums = [1, 2, 3] target = 4 所有可能的组合为: (1, 1, 1, ...

  6. Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III)

    Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III) 同类题目: Leetcode之回溯法专题-39. 组合总数(Combination Sum) Lee ...

  7. Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)

    Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...

  8. [LeetCode] 39. 组合总和

    题目链接 : https://leetcode-cn.com/problems/combination-sum/ 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ...

  9. LeetCode刷题笔记-回溯法-组合总和问题

    题目描述: <组合总和问题>给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. cand ...

随机推荐

  1. vue使用技巧

    引入外部js文件 1.在根目录创建文件夹,例如‘libs’,将js文件拷贝至libs目录下 2.修改webpack.dev.conf.js和webpack.prod.conf.js,在CopyWebp ...

  2. 解惑:如何使得寝室的电脑和实验室的电脑远程相互访问(Linux和Windows)

    解惑:如何使得寝室的电脑和实验室的电脑远程相互访问 一.前言 自从接触计算机网络之后就一直想着把实验室的电脑和自己寝室的电脑远程连接起来,结果总是郁郁不能成功,网上这样的教材也少的可怜,于是总是搁置下 ...

  3. mysql事务隔离级别与设置

    mysql数据库,当且仅当引擎是InnoDB,才支持事务: 1.隔离级别 事务的隔离级别分为:未提交读(read uncommitted).已提交读(read committed).可重复读(repe ...

  4. (三十二)golang--面向对象之封装

    封装:把抽象出来的字段和对字段的操作封装在一起,数据被保护在内部,程序的其它包只有通过被授权的操作(方法),才能对字段进行操作. 封装的好处: (1)隐藏实际的细节: (2)可以对数据进行验证,保证安 ...

  5. Debug 路漫漫-15:Python: NameError:name 'dataset' is not defined

    在调试 <Outer Product-based Neural Collaborative Filtering>论文的源码(https://github.com/duxy-me/ConvN ...

  6. 论文阅读: v-charge项目: 电动车的自动泊车和充电

    Abstract AVP服务会缓和电动车现有两个缺点: 有限的行驶范围和很长的充电时间. v-charge用相机和超声波在GPS-denied的区域全自动形式. 这篇paper叙述了下述几方面的优势: ...

  7. Window权限维持(六):BITS Jobs

    Windows操作系统包含各种实用程序,系统管理员可以使用它们来执行各种任务.这些实用程序之一是后台智能传输服务(BITS),它可以促进文件到Web服务器(HTTP)和共享文件夹(SMB)的传输能力. ...

  8. SpringBoot多数据源动态切换数据源

    1.配置多数据源 spring: datasource: master: password: erp_test@abc url: jdbc:mysql://127.0.0.1:3306/M201911 ...

  9. Spring整合Mybaits java.sql.SQLException: Access denied for user '***'@'localhost' (using password: YES)

    最近在搞Spring和Mybatis的整合,当我们在Spring里面配置数据源,而数据源是从外部的properties文件读取过来的时候就会报错 java.sql.SQLException: Acce ...

  10. @property与@xxx.setter的用法

    类中@property与@xxx.setter的方法介绍. 简单说,@property就是将定义的函数(方法)当作属性对象使用,不需要像调用函数那样去调用,而@xxx.setter是为@xxx的这样函 ...