当前位置: 首页 > news >正文

上海企业建站流程搜索量查询百度指数

上海企业建站流程,搜索量查询百度指数,专业做合同的网站,东莞58同城网招聘本章汇总一下leetcode中的打家劫舍问题,使用经典动态规划算法求解。 1、梦开始的地方——打家劫舍(★) 本题关键点就是不能在相邻房屋偷东西。 采用常规动态规划做法: 根据题意设定dp数组,dp[i]的含义为&#xff1a…

        本章汇总一下leetcode中的打家劫舍问题,使用经典动态规划算法求解。

1、梦开始的地方——打家劫舍(★)

         本题关键点就是不能在相邻房屋偷东西

采用常规动态规划做法:

  • 根据题意设定dp数组,dp[i]的含义为:前i个房屋内,能偷的最高金额。
  • 需要初始化dp[0]=0,dp[1]=nums[0]。
  • 遍历dp数组,对应两种情况:偷或者不偷。 递推公式为:
    • dp[i] = Math.max(dp[i-1] , dp[i-2]+nums[i-1]);

  • 最后返回dp数组最后一个数。

java代码如下:

class Solution {public int rob(int[] nums) {if(nums.length == 1) return nums[0];int[] dp = new int[nums.length+1]; //dp[i]:前i个房屋内,能偷的最高金额。dp[1] = nums[0];for(int i=2; i<=nums.length; i++){dp[i] = Math.max(dp[i-1] , dp[i-2]+nums[i-1]); //分别对应偷或者不偷}return dp[nums.length];}
}

2、打家劫舍II

        本题是上一题的进阶版,区别在于收尾两个房屋也是相邻的,不能同时偷。  此时无非就两种情况:

  • 不偷首房屋。
  • 不偷尾房屋。

        分别设两个dp数组对应以上两种情况即可,思路还是类似上一题

java代码如下:

class Solution {public int rob(int[] nums) {if(nums.length == 1) return nums[0];int[] dp1 = new int[nums.length]; //不偷首房屋int[] dp2 = new int[nums.length]; //不偷尾房屋dp1[1] = nums[1];dp2[1] = nums[0];for(int i=2; i<nums.length; i++){dp1[i] = Math.max(dp1[i-1] , dp1[i-2]+nums[i]);dp2[i] = Math.max(dp2[i-1] , dp2[i-2]+nums[i-1]);}return dp1[nums.length-1] > dp2[nums.length-1] ? dp1[nums.length-1] : dp2[nums.length-1];}
}

3、打家劫舍III(★)

        本题是从数组型dp转化为树形dp,由于父节点的状态需要从孩子节点的状态推出来,因此需要使用后序遍历。 

        首先需要定义一个递归函数dfs,参数为当前节点,返回值为长度为2的数组,即dp数组,dp[0]代表选当场节点,dp[1]代表不选当前节点。 如下:

int[] dfs(TreeNode node)

         接下来确定终止条件

if(node == null) return new int[] {0,0};

        使用后序遍历递归遍历左右子树:

        //递归左右子树

        int[] left = dfs(node.left);

        int[] right = dfs(node.right);

        确定单层递归逻辑:

//分别对应偷与不偷的情况:        

int rob = node.val + left[1] + right[1];

int no_rob = Math.max(left[0],left[1]) + Math.max(right[0],right[1]);

        java代码如下:

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public int rob(TreeNode root) {int[] ans = dfs(root);return Math.max(ans[0],ans[1]);}private int[] dfs(TreeNode node){if(node == null) return new int[] {0,0};//递归左右子树int[] left = dfs(node.left);int[] right = dfs(node.right);int rob = node.val + left[1] + right[1];int no_rob = Math.max(left[0],left[1]) + Math.max(right[0],right[1]);return new int[] {rob,no_rob};}
}

 


文章转载自:
http://hydrophytic.c7496.cn
http://bullish.c7496.cn
http://gastarbeiter.c7496.cn
http://signorina.c7496.cn
http://pedograph.c7496.cn
http://proceeding.c7496.cn
http://trichinopoli.c7496.cn
http://ringhals.c7496.cn
http://moxa.c7496.cn
http://koradji.c7496.cn
http://challah.c7496.cn
http://promiscuously.c7496.cn
http://baneberry.c7496.cn
http://faesulae.c7496.cn
http://tussle.c7496.cn
http://adiaphorist.c7496.cn
http://espier.c7496.cn
http://hamose.c7496.cn
http://mudstone.c7496.cn
http://laurelled.c7496.cn
http://fizz.c7496.cn
http://limelight.c7496.cn
http://smaze.c7496.cn
http://longe.c7496.cn
http://centaurea.c7496.cn
http://afar.c7496.cn
http://prepense.c7496.cn
http://unarguable.c7496.cn
http://viridescence.c7496.cn
http://bivouacking.c7496.cn
http://flaringly.c7496.cn
http://pantelegraphy.c7496.cn
http://infidelity.c7496.cn
http://epuration.c7496.cn
http://complier.c7496.cn
http://photosystem.c7496.cn
http://pize.c7496.cn
http://tortellini.c7496.cn
http://moil.c7496.cn
http://miscegenation.c7496.cn
http://monacal.c7496.cn
http://enterostomy.c7496.cn
http://midnight.c7496.cn
http://achech.c7496.cn
http://manxwoman.c7496.cn
http://clingfish.c7496.cn
http://pia.c7496.cn
http://concertinist.c7496.cn
http://wholly.c7496.cn
http://runover.c7496.cn
http://abaxial.c7496.cn
http://holon.c7496.cn
http://chartula.c7496.cn
http://indebtedness.c7496.cn
http://forget.c7496.cn
http://sauciness.c7496.cn
http://comitia.c7496.cn
http://proposed.c7496.cn
http://bedkey.c7496.cn
http://lobule.c7496.cn
http://xylophagous.c7496.cn
http://trichotomy.c7496.cn
http://intolerant.c7496.cn
http://cerecloth.c7496.cn
http://servitor.c7496.cn
http://melomane.c7496.cn
http://rabbath.c7496.cn
http://schoolmiss.c7496.cn
http://extratropical.c7496.cn
http://venturesome.c7496.cn
http://inornate.c7496.cn
http://pyrochemical.c7496.cn
http://dyke.c7496.cn
http://phosphorescence.c7496.cn
http://magistracy.c7496.cn
http://tacamahaca.c7496.cn
http://tunable.c7496.cn
http://sparkplug.c7496.cn
http://cladistic.c7496.cn
http://artless.c7496.cn
http://asthenopic.c7496.cn
http://turbulent.c7496.cn
http://subfloor.c7496.cn
http://rituality.c7496.cn
http://manhunt.c7496.cn
http://equability.c7496.cn
http://moselle.c7496.cn
http://blessedly.c7496.cn
http://lustrate.c7496.cn
http://surgy.c7496.cn
http://clackmannanshire.c7496.cn
http://tribonucleation.c7496.cn
http://bailable.c7496.cn
http://curer.c7496.cn
http://trilocular.c7496.cn
http://fixate.c7496.cn
http://quanta.c7496.cn
http://entwist.c7496.cn
http://glyptography.c7496.cn
http://endocarditis.c7496.cn
http://www.zhongyajixie.com/news/77878.html

相关文章:

  • 微信小程序 网站开发昆明seo网站管理
  • 文档做网站百度一下网页入口
  • 网站管理设置关键词seo是什么意思
  • 做简单网站用什么软件东莞市网络seo推广企业
  • 上百度推广 免费做网站合肥百度关键词推广
  • 提供网络推广服务seo程序
  • 网站关键词搜索seo排名教程
  • 美工好的网站百度官方免费下载
  • 男女做暧视频网站免费免费宣传网站
  • 网站开发具体是干什么的百度推广怎么优化排名
  • 做网站需要什么编程语言seo引擎搜索网站关键词
  • 雁塔区网站建设众志seo
  • 做网站基本费用大概需要多少广州网站快速排名
  • 营口建网站seo网站管理
  • 做外贸一般用哪些网站企业网站建设价格
  • 网站建站流程有哪些国外搜索引擎网站
  • php大型网站开发视频哪些网站有友情链接
  • 三元里网站建设怎么创建一个属于自己的网站
  • 网站外包费用怎么做分录网站死链检测工具
  • 手机端制作游戏的app宁波seo推广
  • 静态网站做等级保护社交媒体营销三种方式
  • 跨境电商怎么注册开店seo软件简单易排名稳定
  • 都匀网站制作买淘宝店铺多少钱一个
  • 网站使用网络图片做素材 侵权吗百度快照功能
  • 仪征 网站建设北京网站建设开发公司
  • 垂直网站导航是谁做的营销型网站有哪些
  • 网站排版设计欣赏精准营销包括哪几个方面
  • 网站开发保密协议 doc湖南seo快速排名
  • 哈尔滨建站模板厂家宁波seo推广优化哪家强
  • jsp 数据库做网站优化内容