题目:

关于动态规划类题目的思路如何找在上一篇博客 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. Python Dataframe 分组排序和 Modin

    Python Dataframe 分组排序和 Modin 1.按照其中一列进行排序 在dataframe中,按照其中的一列排序:比如q值倒排 (1)rank方法 data['new_rank'] = ...

  2. 修改kile工程名字(转)

    假设原来的工程文件名是first,要改成second1.在工程文件目录中,将first.uvopt和first.uvproj名字改成second.uvopt和second.uvproj.2.其他fir ...

  3. 深入理解Java8中Stream的实现原理

    Stream Pipelines 前面我们已经学会如何使用Stream API,用起来真的很爽,但简洁的方法下面似乎隐藏着无尽的秘密,如此强大的API是如何实现的呢?比如Pipeline是怎么执行的, ...

  4. 'GL_EXT_shader_framebuffer_fetch' : extension is not supported

    在使用安卓模拟器加载Flutter应用时, 提示'GL_EXT_shader_framebuffer_fetch' : extension is not supported: D/skia (1404 ...

  5. vue中mode hash 和 history的区别

    对于 Vue 这类渐进式前端开发框架,为了构建 SPA(单页面应用),需要引入前端路由系统,这也就是 Vue-Router 存在的意义.前端路由的核心,就在于 —— 改变视图的同时不会向后端发出请求. ...

  6. 解决python3.7 ModuleNotFoundError: No module named bz2

    解决: ModuleNotFoundError: No module named  bz2 ModuleNotFoundError: No module named '_lzma' 1.在操作系统中安 ...

  7. PHP自动加载-spl_autoload_register

    spl_autoload_register 自动加载spl : Standard PHP library (标准PHP库) 首先来了解 __autoload print.class.php <? ...

  8. navicat远程连接mysql的方法

    navicat远程连接mysql的方法1 先在打开phpmyadmin 添加用户 用户名和密码自己设置 设置如下 2 关闭防火墙service iptables status可以查看到iptables ...

  9. java jdb

    https://stackoverflow.com/questions/8155253/how-do-i-compile-in-debug-mode-netbeans-java-maven <p ...

  10. Activex在没有电子秤api的情况下获取串口数据

    大二做B/S架构的项目使用了安衡电子秤CHS-D+R和一款扫码枪,两个设备的串口使用一样,这款电子秤是相当的坑,没有开发的api,无奈只能自己开发Activex了,在B/S架构中进行引用Activex ...