题目:

关于动态规划类题目的思路如何找在上一篇博客 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. Appium 环境配置

    前言 Appium 作为移动端自动化测试工具在业界非常流行,特别是在当前移动互联网背景下,很多公司基于此框架来开展自动化测试.但 appium 的环境配置相对 selenium 来说复杂的多,让很多同 ...

  2. 明解C语言 中级篇 第四章答案

    练习4-1 /* 珠玑妙算 */ #include <time.h> #include <ctype.h> #include <stdio.h> #include ...

  3. 【06】Nginx:文件下载 / 用户认证

    写在前面的话 在公司内部一般都会存在 FTP / SAMBA 这样类似的文件服务器,虽然这类的程序都可以对用户的权限进行控制,但我们有时候其实只需要一个简单的下载页面,类似软件仓库.用户不管在哪里打开 ...

  4. NET Excel转换为集合对象

    1.仅适用于规则Excel:表头和数据一一对应 2.涉及到Excel转换为集合对象的部分代码,完整npoi帮助类点击查看 /// <summary> /// 默认把excel第一个shee ...

  5. Dev控件使用CheckedListBoxControl获取items.count为0 的解决方法

    CheckedListBoxControl,我使用DataSource属性,给其绑定了一个List对象.界面显示都挺正常的,当若干个项的复选框被选中的后,它的checkedListBoxControl ...

  6. 关于 L3 缓存行 cacheLIne 的研究!还是对程序有举足轻重的作用!

    https://www.cnblogs.com/PurpleTide/archive/2010/11/25/1887506.html CLR via C# 读书笔记 2-3 Cache Lines a ...

  7. easyui高级控件

    开发模式 1. 美工(ui工程师:出一个项目模型) java工程师:将原有的html转成jsp,动态展示数据 缺点: 客户需要调节前端的展示效果 解决:由美工去重新排版,重新选色.2.前后端分离 美工 ...

  8. 【转】Python学习---超详细字符串用法大全,好文推荐!

    来自:Python编程与实战(微信号:pthon1024),作者:Jerryning 没有办法转,整个复制下来了 本文要点 字符串拼接 拆分含有多种分隔符的字符串 判读字符串a是否以字符串b开头或结尾 ...

  9. flux架构的详细介绍和使用!

    结构分为四个 视图 view动作 action派发器 dispatcher数据商店 store 流程: 用户操作视图 视图(view)发送动作(action)到派发器(dispatcher) 由派发器 ...

  10. es6中find方法

    find() 方法返回数组中满足提供的测试函数的第一个元素的值.否则返回 undefined. , , , , ]; var found = array1.find(function(element) ...