题目:

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

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

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

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

递归搜索解法:

  1. /**
  2. * @Author Nxy
  3. * @Date 2019/12/21
  4. * @Param
  5. * @Return
  6. * @Exception
  7. * @Description 递归搜索
  8. */
  9. int i = 0;
  10.  
  11. public int combinationSum4(int[] nums, int target) {
  12. if (nums == null) {
  13. return 0;
  14. }
  15. combinationSum4(nums, 0, target);
  16. return i;
  17. }
  18.  
  19. public void combinationSum4(int[] nums, int beforeRe, int target) {
  20. if (beforeRe > target) {
  21. return;
  22. }
  23. if (beforeRe == target) {
  24. i++;
  25. return;
  26. }
  27. int length = nums.length;
  28. for (int i = 0; i < length; i++) {
  29. int tempRe = beforeRe + nums[i];
  30. combinationSum4(nums, tempRe, target);
  31. }
  32. }

分治解法:

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

  1. /**
  2. * @Author Nxy
  3. * @Date 2019/12/21
  4. * @Param
  5. * @Return
  6. * @Exception
  7. * @Description 分治加缓存
  8. */
  9. public int combinationSum4II(int[] nums, int target) {
  10. if (nums == null) {
  11. return 0;
  12. }
  13. int length = nums.length;
  14. Map<Integer, Integer> cache = new HashMap<Integer, Integer>();
  15. return combinationSum4II(nums, target, length, cache);
  16. }
  17.  
  18. public int combinationSum4II(int[] nums, int target, int length, Map<Integer, Integer> cache) {
  19. if (target < 0) {
  20. return 0;
  21. }
  22. if (target == 0) {
  23. return 1;
  24. }
  25. Set s = cache.keySet();
  26. if (s.contains(target)) {
  27. return cache.get(target);
  28. }
  29. int temp = 0;
  30. for (int i = 0; i < length; i++) {
  31. temp += combinationSum4II(nums, target - nums[i], length, cache);
  32. }
  33. cache.put(target, temp);
  34. return temp;
  35. }

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

 动态规划解法:

  1. /**
  2. * @Author Nxy
  3. * @Date 2019/12/21
  4. * @Param
  5. * @Return
  6. * @Exception
  7. * @Description DP解法
  8. */
  9. public int combinationSum4III(int[] nums, int target){
  10. if(nums==null){return 0;}
  11. int length=nums.length;
  12. int[] cache=new int[target+1];
  13. cache[0]=1;
  14. for(int i=1;i<=target;i++){
  15. int temp=0;
  16. for(int j=0;j<length;j++){
  17. if(i-nums[j]==0){
  18. temp++;
  19. continue;
  20. }
  21. if(i-nums[j]>0){
  22. temp+=cache[i-nums[j]];
  23. }
  24. }
  25. cache[i]=temp;
  26. }
  27. return cache[target];
  28. }

效率提升:

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

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. mysql小知识点汇总---(时间与时间戳的转换, 修改mysql用户名密码, navicate 导入sql文件报错 1153)

    1. 时间与时间戳的转换 1.1 时间戳转时间 FROM_UNIXTIME(add_time, '%Y-%m-%d') 1.2 时间转时间戳 UNIX_TIMESTAMP('2015-04-29') ...

  2. HTTP协议第一篇:三握手解读

    TCP(Transmission Control Protocol) 传输控制协议 TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: 位码即tcp标志位,有6种标 ...

  3. 【Java并发专题之二】Java线程基础

    使用线程更好的提高资源利用率,但也会带来上下文切换的消耗,频繁的内核态和用户态的切换消耗,如果代码设计不好,可能弊大于利. 一.线程 进程是分配资源的最小单位,线程是程序执行的最小单位:线程是依附于进 ...

  4. 《一起学mysql》5

    基准函数   用于评估不同机器之间的性能差别   MariaDB [jason]> select benchmark(10000000,md5('test')); +-------------- ...

  5. [LeetCode#180]Consecutive Numbers

    Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...

  6. ML.NET调用Tensorflow模型示例——MNIST

    ML.NET在不久前发行了1.0版本,在考虑这一新轮子的实际用途时,最先想到的是其能否调用已有的模型,特别是最被广泛使用的Tensorflow模型.于是在查找了不少资料后,有了本篇示例.希望可以有抛砖 ...

  7. MS14-068域提权漏洞复现

    MS14-068域提权漏洞复现 一.漏洞说明 改漏洞可能允许攻击者将未经授权的域用户账户的权限,提权到域管理员的权限. 微软官方解释: https://docs.microsoft.com/zh-cn ...

  8. Java生鲜电商平台-会员积分系统的设计与架构

    Java生鲜电商平台-会员积分系统的设计与架构 说明:互联网平台积分体系主要用于激励和回馈用户在平台的消费行为和活动行为,一个良好的积分体系可以很好的提升用户的粘性及活跃度. 一.互联网平台积分体系设 ...

  9. python网络编程-1

    1.网络基础 回顾计算IP所处网段方式 #128 64 32 16 8 4 2 1 #IP1 = 192.168.9.1/24 # 11000000 10101000 00001001 0000000 ...

  10. global对象,数据存储方式和检测,包装器对象等

    1.理解global对象 global对象是作为 window 对象的一部分实现的,我们无法通过代码访问到 global 对象. 我们平时在全局环境下定义的内容(变量,函数,常量等等)都是作为 glo ...