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 ...
随机推荐
- Python Dataframe 分组排序和 Modin
Python Dataframe 分组排序和 Modin 1.按照其中一列进行排序 在dataframe中,按照其中的一列排序:比如q值倒排 (1)rank方法 data['new_rank'] = ...
- 修改kile工程名字(转)
假设原来的工程文件名是first,要改成second1.在工程文件目录中,将first.uvopt和first.uvproj名字改成second.uvopt和second.uvproj.2.其他fir ...
- 深入理解Java8中Stream的实现原理
Stream Pipelines 前面我们已经学会如何使用Stream API,用起来真的很爽,但简洁的方法下面似乎隐藏着无尽的秘密,如此强大的API是如何实现的呢?比如Pipeline是怎么执行的, ...
- 'GL_EXT_shader_framebuffer_fetch' : extension is not supported
在使用安卓模拟器加载Flutter应用时, 提示'GL_EXT_shader_framebuffer_fetch' : extension is not supported: D/skia (1404 ...
- vue中mode hash 和 history的区别
对于 Vue 这类渐进式前端开发框架,为了构建 SPA(单页面应用),需要引入前端路由系统,这也就是 Vue-Router 存在的意义.前端路由的核心,就在于 —— 改变视图的同时不会向后端发出请求. ...
- 解决python3.7 ModuleNotFoundError: No module named bz2
解决: ModuleNotFoundError: No module named bz2 ModuleNotFoundError: No module named '_lzma' 1.在操作系统中安 ...
- PHP自动加载-spl_autoload_register
spl_autoload_register 自动加载spl : Standard PHP library (标准PHP库) 首先来了解 __autoload print.class.php <? ...
- navicat远程连接mysql的方法
navicat远程连接mysql的方法1 先在打开phpmyadmin 添加用户 用户名和密码自己设置 设置如下 2 关闭防火墙service iptables status可以查看到iptables ...
- java jdb
https://stackoverflow.com/questions/8155253/how-do-i-compile-in-debug-mode-netbeans-java-maven <p ...
- Activex在没有电子秤api的情况下获取串口数据
大二做B/S架构的项目使用了安衡电子秤CHS-D+R和一款扫码枪,两个设备的串口使用一样,这款电子秤是相当的坑,没有开发的api,无奈只能自己开发Activex了,在B/S架构中进行引用Activex ...