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 ...
随机推荐
- mysql小知识点汇总---(时间与时间戳的转换, 修改mysql用户名密码, navicate 导入sql文件报错 1153)
1. 时间与时间戳的转换 1.1 时间戳转时间 FROM_UNIXTIME(add_time, '%Y-%m-%d') 1.2 时间转时间戳 UNIX_TIMESTAMP('2015-04-29') ...
- HTTP协议第一篇:三握手解读
TCP(Transmission Control Protocol) 传输控制协议 TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: 位码即tcp标志位,有6种标 ...
- 【Java并发专题之二】Java线程基础
使用线程更好的提高资源利用率,但也会带来上下文切换的消耗,频繁的内核态和用户态的切换消耗,如果代码设计不好,可能弊大于利. 一.线程 进程是分配资源的最小单位,线程是程序执行的最小单位:线程是依附于进 ...
- 《一起学mysql》5
基准函数 用于评估不同机器之间的性能差别 MariaDB [jason]> select benchmark(10000000,md5('test')); +-------------- ...
- [LeetCode#180]Consecutive Numbers
Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...
- ML.NET调用Tensorflow模型示例——MNIST
ML.NET在不久前发行了1.0版本,在考虑这一新轮子的实际用途时,最先想到的是其能否调用已有的模型,特别是最被广泛使用的Tensorflow模型.于是在查找了不少资料后,有了本篇示例.希望可以有抛砖 ...
- MS14-068域提权漏洞复现
MS14-068域提权漏洞复现 一.漏洞说明 改漏洞可能允许攻击者将未经授权的域用户账户的权限,提权到域管理员的权限. 微软官方解释: https://docs.microsoft.com/zh-cn ...
- Java生鲜电商平台-会员积分系统的设计与架构
Java生鲜电商平台-会员积分系统的设计与架构 说明:互联网平台积分体系主要用于激励和回馈用户在平台的消费行为和活动行为,一个良好的积分体系可以很好的提升用户的粘性及活跃度. 一.互联网平台积分体系设 ...
- python网络编程-1
1.网络基础 回顾计算IP所处网段方式 #128 64 32 16 8 4 2 1 #IP1 = 192.168.9.1/24 # 11000000 10101000 00001001 0000000 ...
- global对象,数据存储方式和检测,包装器对象等
1.理解global对象 global对象是作为 window 对象的一部分实现的,我们无法通过代码访问到 global 对象. 我们平时在全局环境下定义的内容(变量,函数,常量等等)都是作为 glo ...