题目:

关于动态规划类题目的思路如何找在上一篇博客 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];
}

效率提升:

递归太费时,我们单独看下分治到动态规划的效率提升:

最新文章

  1. angular2系列教程(七)Injectable、Promise、Interface、使用服务
  2. Windows下PowerShell监控Keepalived
  3. Unity3D 装备系统学习Inventory Pro 2.1.2 基础篇
  4. Linux vi/vim
  5. 【easuyi】---easyui中的验证validatebox自定义
  6. HDU 5676 ztr loves lucky numbers (模拟)
  7. 关于开源的RTP——jrtplib的使用
  8. HDU 1087 Super Jumping! Jumping! Jumping!(动态规划)
  9. 字符串处理,NSNumber转换
  10. JavaScript看书笔记01
  11. Android播放图片动画
  12. VirtualBox查看虚拟机IP地址
  13. 【Android】为需要支持API 11之前的Activity添加Action Bar的一种解决方案
  14. VirtualBox虚拟机安装Mac OS 10.12
  15. 2019.03.25 Ajax三级联动
  16. JSP中的Java代码和内置对象
  17. VS2015 之 常用快捷键
  18. 老男孩linux实战培训初级班第二次课前考试题
  19. vue-cli打包之后的项目在nginx的部署
  20. 20165333 实验二 Java面向对象程序设计

热门文章

  1. [LeetCode] 84. Largest Rectangle in Histogram 直方图中最大的矩形
  2. bootstrap-switch使用
  3. app版本升级的测试点
  4. webstorm关闭烦人的eslint语法检查
  5. docker安装kafka
  6. C# 方法默认访问级别 : private C# 类默认访问级别 : internal
  7. 基于log4net的日志组件扩展封装,实现自动记录交互日志 XYH.Log4Net.Extend(微服务监控)
  8. webpack4 babel 篇
  9. springmvc在使用@ModelAttribute注解获取Request和Response会产生线程并发不安全问题(转)
  10. MQ选型对比ActiveMQ,RabbitMQ,RocketMQ,Kafka 消息队列框架选哪个?