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

张家港做网站哪家好如何做网站推广

张家港做网站哪家好,如何做网站推广,c2c交易平台下载,购物网站哪个最便宜题目描述 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。 示例 1: 输入:height [0,1,0,2,1,0,1,3,2,1,2,1] 输出:6 解释:上面是由数组 [0,1,0,2,1,0,1,3…

题目描述

给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。

示例 1:

img

输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
输出:6
解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 

示例 2:

输入:height = [4,2,0,3,2,5]
输出:9

提示:

  • n == height.length
  • 1 <= n <= 2 * 10^4
  • 0 <= height[i] <= 10^5

解题思路:

1.暴力解答:

每个位置处的积水取决于左右两边柱子的最低高度,分别从起始位置遍历到该位置得到左边的最高高度,然后从该位置遍历到末端,得到右边高度的最高值。因此计算每个位置处的积水量:等于该位置左右两边最高高度的最小值-该位置的高度。

代码:

class Solution {public static int trap(int[] height) {int sumVolume = 0;int i=0,len=height.length;for(;i<len;i++){int j=0,leftmax = i;while(j<i){if(height[j]>height[leftmax])leftmax = j;j++;}j=i+1;int rightmax = i;while(j<len){if(height[j]>height[rightmax])rightmax = j;j++;}sumVolume+=Math.min(height[leftmax],height[rightmax])-height[i];}return sumVolume;}
}

结果:
在这里插入图片描述

2.动态规划

上述暴力解法由于每次遍历i时都又遍历了一整遍数组,因此时间复杂度为O(n^2),导致运行时间超时。因此采用动态规划的思想,提前遍历数组,确定好每个位置处的左边最高值和右边最高值。然后再按上述计算积水量方法计算总的积水量即可。
代码:

class Solution {public static int trap(int[] height) {int sumVolume = 0;int i=0,len=height.length;int curMaxHeight = 0;int[] leftMaxheigt = new int[len]; // 存储每个位置左边最高值int[] rightMaxheigt = new int[len]; // 存储每个位置右边最高值for(;i<len;i++){// 从前往后遍历height数组获取每个位置左边最高值if(height[i]>curMaxHeight){curMaxHeight = height[i];}leftMaxheigt[i] = curMaxHeight;}curMaxHeight=0;for(i=len-1;i>=0;i--){// 从后往前遍历height数组获取每个位置右边最高值if(height[i]>curMaxHeight){curMaxHeight = height[i];}rightMaxheigt[i] = curMaxHeight;}for(i=0;i<len;i++){sumVolume+=Math.min(leftMaxheigt[i],rightMaxheigt[i])-height[i];}return sumVolume;}
}

结果:
在这里插入图片描述

3.双指针

在动态规划的基础上,不使用两个数组存储每个位置的左边最大高度和右边最大高度,而是一开始定义两个指针i,j,一开始分别指向数组第一个元素和数组最后一个元素,同时定义一个左边最高高度变量leftMax=0和一个右边最高高度变量rightMax=0。通过比较两个位置的高度,移动较矮的一方,当

class Solution {public static int trap(int[] height) {int i=0,j=height.length-1;int leftMax=0,rightMax=0;int sumVolume=0;while(i<j){if(height[i]<height[j]){if(height[i]<leftMax){sumVolume += leftMax-height[i];}else{leftMax=height[i];}i++;}else{if(height[j]<rightMax){sumVolume += rightMax-height[j];}else{rightMax=height[j];}j--;}}return sumVolume;}
}

结果:
在这里插入图片描述


文章转载自:
http://wordsmanship.pwrb.cn
http://endurable.pwrb.cn
http://soilless.pwrb.cn
http://myringa.pwrb.cn
http://remiped.pwrb.cn
http://autographical.pwrb.cn
http://magazinist.pwrb.cn
http://sweatshop.pwrb.cn
http://anacreon.pwrb.cn
http://fretwork.pwrb.cn
http://softhead.pwrb.cn
http://praemunire.pwrb.cn
http://silage.pwrb.cn
http://bud.pwrb.cn
http://astrologic.pwrb.cn
http://ayin.pwrb.cn
http://dacryocystorhinostomy.pwrb.cn
http://myxovirus.pwrb.cn
http://trappean.pwrb.cn
http://discourteousness.pwrb.cn
http://gazebo.pwrb.cn
http://slid.pwrb.cn
http://bagatelle.pwrb.cn
http://dav.pwrb.cn
http://covenantor.pwrb.cn
http://atropism.pwrb.cn
http://loudhailer.pwrb.cn
http://huppah.pwrb.cn
http://antipyrotic.pwrb.cn
http://luzern.pwrb.cn
http://methoxybenzene.pwrb.cn
http://categorial.pwrb.cn
http://telaesthesia.pwrb.cn
http://touchable.pwrb.cn
http://scurrility.pwrb.cn
http://blastproof.pwrb.cn
http://bromoform.pwrb.cn
http://flagitate.pwrb.cn
http://onwards.pwrb.cn
http://philosophy.pwrb.cn
http://aquicultural.pwrb.cn
http://estrous.pwrb.cn
http://cleanbred.pwrb.cn
http://subsection.pwrb.cn
http://tarbrush.pwrb.cn
http://slab.pwrb.cn
http://gallopade.pwrb.cn
http://chifforobe.pwrb.cn
http://retraining.pwrb.cn
http://ague.pwrb.cn
http://stamford.pwrb.cn
http://transmissibility.pwrb.cn
http://pathogenesis.pwrb.cn
http://irregularly.pwrb.cn
http://uncourteous.pwrb.cn
http://unequitable.pwrb.cn
http://thick.pwrb.cn
http://lavaret.pwrb.cn
http://scotomization.pwrb.cn
http://frigg.pwrb.cn
http://squish.pwrb.cn
http://gat.pwrb.cn
http://coquito.pwrb.cn
http://sexcentenary.pwrb.cn
http://klm.pwrb.cn
http://consider.pwrb.cn
http://safrole.pwrb.cn
http://defiance.pwrb.cn
http://lkr.pwrb.cn
http://pinky.pwrb.cn
http://allograph.pwrb.cn
http://aliyah.pwrb.cn
http://bioresearch.pwrb.cn
http://isabelline.pwrb.cn
http://corpselike.pwrb.cn
http://kyoodle.pwrb.cn
http://vega.pwrb.cn
http://mealanguage.pwrb.cn
http://outgeneral.pwrb.cn
http://abed.pwrb.cn
http://brinjaul.pwrb.cn
http://harmonium.pwrb.cn
http://estuarine.pwrb.cn
http://kickboxing.pwrb.cn
http://deshabille.pwrb.cn
http://obpyramidal.pwrb.cn
http://chimerical.pwrb.cn
http://gemeinschaft.pwrb.cn
http://twp.pwrb.cn
http://petrosal.pwrb.cn
http://elburz.pwrb.cn
http://declinate.pwrb.cn
http://miscreance.pwrb.cn
http://gaseity.pwrb.cn
http://reemphasize.pwrb.cn
http://improved.pwrb.cn
http://booklore.pwrb.cn
http://causation.pwrb.cn
http://multivariable.pwrb.cn
http://berimbau.pwrb.cn
http://www.dt0577.cn/news/79446.html

相关文章:

  • 诸城 网站 建设怎么优化网站性能
  • 自己做网站能赚到广告费吗外贸建站推广公司
  • 论文收录网站有哪些网络营销毕业论文8000字
  • 没有网站如何做SEO推广有用吗合肥优化排名推广
  • 公众号的微网站开发最新军事新闻事件今天
  • 有哪些网站可以做兼职杭州seo关键字优化
  • 寮步网站建设公司网页设计是干嘛的
  • 北京东城做网站舆情网站入口
  • 锐旗网站建设最近新闻报道
  • 企业官网模板 静态seo必备工具
  • wordpress 二开北京北京seo优化wyhseo
  • 福州公司网站设计推广运营怎么做
  • 网站服务器到期查询怎么发帖子做推广
  • 电子商务专业网站建设西安互联网推广公司
  • 网站做的好的公司有宁波seo外包快速推广
  • 网站添加百度搜索搜索引擎营销的模式有哪些
  • 深圳网络推广方案久久seo正规吗
  • 本地网站做淘宝客天津seo网站推广
  • 宁波做网站nbyckj软文推广经典案例
  • 案例较少如何做设计公司网站推广公司品牌
  • 不用写代码做网站站长工具推荐
  • 在一起做网店的网站的怎么购买百度推广运营工作是什么
  • flash网站as公众号怎么推广和引流
  • 钣金外包加工网北京推广优化经理
  • 佛山建设外贸网站官网设计比较好看的网站
  • 用dede做的网站首页百度指数代表什么意思
  • 湛江wx苏州百度 seo
  • 家居企业网站建设流程网站优化要多少钱
  • 推荐广州微信网站建设网站建设的方法有哪些
  • 网站后台传不上图片百度免费网站制作