leetcode组合总和 Ⅳ 解题路径

题目:

关于动态规划类题目的思路如何找在上一篇博客 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组合总和 Ⅳ 解题路径的更多相关文章
- 图解Leetcode组合总和系列——回溯(剪枝优化)+动态规划
Leetcode组合总和系列--回溯(剪枝优化)+动态规划 组合总和 I 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 ...
- 34,Leetcode 组合总和I,II -C++ 回溯法
I 题目描述 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的数字可以无 ...
- LeetCode 组合总和(dfs)
题目 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重 ...
- Leetcode题目39.组合总和(回溯+剪枝-中等)
题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无 ...
- Leetcode 377.组合总和IV
组合总和IV 给定一个由正整数组成且不存在重复数字的数组,找出和为给定目标正整数的组合的个数. 示例: nums = [1, 2, 3] target = 4 所有可能的组合为: (1, 1, 1, ...
- Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III)
Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III) 同类题目: Leetcode之回溯法专题-39. 组合总数(Combination Sum) Lee ...
- Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)
Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...
- [LeetCode] 39. 组合总和
题目链接 : https://leetcode-cn.com/problems/combination-sum/ 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ...
- LeetCode刷题笔记-回溯法-组合总和问题
题目描述: <组合总和问题>给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. cand ...
随机推荐
- 生成git的SSH公钥
1.右键,点击 git bash here 2.安装成功后设置用户和邮箱git config --global user.name "name"git config --glob ...
- 第02组 Beta冲刺(4/5)
队名:無駄無駄 组长博客 作业博客 组员情况 张越洋 过去两天完成了哪些任务 讨论校园百科究竟如何实现,并分配了任务 提交记录(全组共用) 接下来的计划 加快校园百科的进度 准备Beta版本的汇报 还 ...
- finalize()方法什么时候被调用?析构函数(finalization)的目的是什么?
链接:https://www.nowcoder.com/questionTerminal/d8eab06913084e42b515633604eef7cd?pos=28&mutiTagIds= ...
- win10 mysql5.7.28 配置安装
如果有服务,使用下面命令删除,管理员身份打开cmd : sc delete mysql 1.下载 https://dev.mysql.com/downloads/mysql/5.7.html 没有的O ...
- 《一起学netty》
o文章摘自 netty 官网(netty.io) netty 是一个异步的,事件驱动的网络应用通信框架,可以让我们快速编写可靠,高性能,高可扩展的服务端和客户端 样例一:discard ser ...
- Flink之state processor api实践
前不久,Flink社区发布了FLink 1.9版本,在其中包含了一个很重要的新特性,即state processor api,这个框架支持对checkpoint和savepoint进行操作,包括读取. ...
- docker容器的端口映射
1.创建一个Nginx 容器,先不映射端口 [root@localhost ~]# docker run --name my_nginx -d nginx 7be3673a4c0f8f7ffe79a7 ...
- Redis学习记录及Jedis代码示例
文章目录 二.Redis简介 三.Redis安装 1. 下载并解压安装 2. 安装C语言编译环境 3. 修改安装位置 4. 编译安装 5.启动Redis服务器 ①默认启动 ②定制配置项启动 [1]准备 ...
- WinForms中动态给treeView的节点添加ContextMenuStrip,并绑定Click事件
生成ContextMenuStrip var docMenu = new ContextMenuStrip(); ToolStripMenuItem deleteMenuItem = new Tool ...
- golang中,slice的几个易混淆点
slice在golang中是最常用的类型,一般可以把它作为数组使用,但是比数组要高效呀.不过,我感觉这个东西用的不好坑太多了.还是需要了解下他底层的实现 slice的结构定义 type slice s ...