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

有什么网站是学做吃的平台运营推广

有什么网站是学做吃的,平台运营推广,深度开发,网站建设与维护A卷答案【leetcode面试经典150题】专栏系列将为准备暑期实习生以及秋招的同学们提高在面试时的经典面试算法题的思路和想法。本专栏将以一题多解和精简算法思路为主,题解使用C语言。(若有使用其他语言的同学也可了解题解思路,本质上语法内容一致&…

【leetcode面试经典150题】专栏系列将为准备暑期实习生以及秋招的同学们提高在面试时的经典面试算法题的思路和想法。本专栏将以一题多解和精简算法思路为主,题解使用C++语言。(若有使用其他语言的同学也可了解题解思路,本质上语法内容一致)

【题目描述】

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

【示例一】

输入: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 个单位的雨水(蓝色部分表示雨水)。 

【示例二】

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

【提示及数据范围】

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

【代码】

// 方法一:动态规划// 对于下标 i,下雨后水能到达的最大高度等于下标 i 两边的最大高度的最小值,
// 下标 i 处能接的雨水量等于下标 i 处的水能到达的最大高度减去 height[i]。
// 创建两个长度为 n 的数组 leftMax 和 rightMax。
// 对于 0≤i<n,leftMax[i] 表示下标 i 及其左边的位置中,height 的最大高度,
// rightMax[i] 表示下标 i 及其右边的位置中,height 的最大高度。// leftMax[0]=height[0],rightMax[n−1]=height[n−1]。两个数组的其余元素的计算如下:// 当 1≤i≤n−1 时,leftMax[i]=max⁡(leftMax[i−1],height[i]);// 当 0≤i≤n−2 时,rightMax[i]=max⁡(rightMax[i+1],height[i])。// 因此可以正向遍历数组 height 得到数组 leftMax 的每个元素值,
// 反向遍历数组 height 得到数组 rightMax 的每个元素值。// 在得到数组 leftMax 和 rightMax 的每个元素值之后,
// 对于 0≤i<n,下标 i 处能接的雨水量等于 min⁡(leftMax[i],rightMax[i])−height[i]。
// 遍历每个下标位置即可得到能接的雨水总量。class Solution {
public:int trap(vector<int>& height) {int n = height.size();if (n == 0) {return 0;}vector<int> leftMax(n);leftMax[0] = height[0];for (int i = 1; i < n; ++i) {leftMax[i] = max(leftMax[i - 1], height[i]);}vector<int> rightMax(n);rightMax[n - 1] = height[n - 1];for (int i = n - 2; i >= 0; --i) {rightMax[i] = max(rightMax[i + 1], height[i]);}int ans = 0;for (int i = 0; i < n; ++i) {ans += min(leftMax[i], rightMax[i]) - height[i];}return ans;}
};//方法二:单调栈// 维护一个单调栈,单调栈存储的是下标,满足从栈底到栈顶的下标对应的数组 height 中的元素递减。// 从左到右遍历数组,遍历到下标 i 时,如果栈内至少有两个元素,记栈顶元素为 top
// top 的下面一个元素是 left,则一定有 height[left]≥height[top]。// 如果 height[i]>height[top],则得到一个可以接雨水的区域,该区域的宽度是 i−left−1,
// 高度是 min⁡(height[left],height[i])−height[top],
// 根据宽度和高度即可计算得到该区域能接的雨水量。// 为了得到 left,需要将 topp 出栈。在对 top 计算能接的雨水量之后,left 变成新的 top,
// 重复上述操作,直到栈变为空,或者栈顶下标对应的 height 中的元素大于或等于 height[i]。// 在对下标 i 处计算能接的雨水量之后,将 i 入栈,
// 继续遍历后面的下标,计算能接的雨水量。遍历结束之后即可得到能接的雨水总量。class Solution {
public:int trap(vector<int>& height) {int ans = 0;stack<int> stk;int n = height.size();for (int i = 0; i < n; ++i) {while (!stk.empty() && height[i] > height[stk.top()]) {int top = stk.top();stk.pop();if (stk.empty()) {break;}int left = stk.top();int currWidth = i - left - 1;int currHeight = min(height[left], height[i]) - height[top];ans += currWidth * currHeight;}stk.push(i);}return ans;}
};// 方法三:双指针//维护两个指针 left 和 right,以及两个变量 leftMax 和 rightMax,
// 初始时 left=0,right=n−1,leftMax=0,rightMax=0。
// 指针 left 只会向右移动,指针 right 只会向左移动,
// 在移动指针的过程中维护两个变量 leftMax 和 rightMax 的值。// 当两个指针没有相遇时,进行如下操作:// 使用 height[left] 和 height[right] 的值更新 leftMax 和 rightMax 的值;// 如果 height[left]<height[right],则必有 leftMax<rightMax,
// 下标 left 处能接的雨水量等于 leftMax−height[left] 处能接的雨水量加到能接的雨水总量,
// 然后将 left 加 1(即向右移动一位);// 如果 height[left]≥height[right],则必有 leftMax≥rightMax,
// 下标 right 处能接的雨水量等于 rightMax−height[right],
// 将下标 right 处能接的雨水量加到能接的雨水总量,然后将 right 减 1(即向左移动一位)。// 当两个指针相遇时,即可得到能接的雨水总量。class Solution {
public:int trap(vector<int>& height) {int ans = 0;int left = 0, right = height.size() - 1;int leftMax = 0, rightMax = 0;while (left < right) {leftMax = max(leftMax, height[left]);rightMax = max(rightMax, height[right]);if (height[left] < height[right]) {ans += leftMax - height[left];++left;} else {ans += rightMax - height[right];--right;}}return ans;}
};

文章转载自:
http://dinncobareness.tpps.cn
http://dinncoascesis.tpps.cn
http://dinncoarachis.tpps.cn
http://dinncosclerotoid.tpps.cn
http://dinncoequilibrator.tpps.cn
http://dinncolocoman.tpps.cn
http://dinncotussock.tpps.cn
http://dinncorefutation.tpps.cn
http://dinncoshortclothes.tpps.cn
http://dinncopreceptory.tpps.cn
http://dinncodecamp.tpps.cn
http://dinncocroatia.tpps.cn
http://dinncopiratic.tpps.cn
http://dinncozara.tpps.cn
http://dinncocaponette.tpps.cn
http://dinncoskylab.tpps.cn
http://dinncoaptotic.tpps.cn
http://dinncorussian.tpps.cn
http://dinncoantifluoridationist.tpps.cn
http://dinncoprivatdocent.tpps.cn
http://dinncopostlady.tpps.cn
http://dinncoironside.tpps.cn
http://dinncoincogitable.tpps.cn
http://dinncowhether.tpps.cn
http://dinncomephenesin.tpps.cn
http://dinncodelilah.tpps.cn
http://dinncohatty.tpps.cn
http://dinncomurrelet.tpps.cn
http://dinncoautogenous.tpps.cn
http://dinncotriweekly.tpps.cn
http://dinncohandicapped.tpps.cn
http://dinncoburp.tpps.cn
http://dinncosky.tpps.cn
http://dinncoorigination.tpps.cn
http://dinncohepatatrophia.tpps.cn
http://dinncorusticism.tpps.cn
http://dinncoarchives.tpps.cn
http://dinncoinformed.tpps.cn
http://dinncosots.tpps.cn
http://dinncoampere.tpps.cn
http://dinncocercopithecoid.tpps.cn
http://dinncoheliogabalus.tpps.cn
http://dinncoparamatta.tpps.cn
http://dinncosupercontinent.tpps.cn
http://dinncocatskinner.tpps.cn
http://dinncoaraeostyle.tpps.cn
http://dinncolenape.tpps.cn
http://dinncotouriste.tpps.cn
http://dinnconewdigate.tpps.cn
http://dinncoconfessed.tpps.cn
http://dinncoleafcutter.tpps.cn
http://dinncojunius.tpps.cn
http://dinncoacestoma.tpps.cn
http://dinncoiconographic.tpps.cn
http://dinncomouth.tpps.cn
http://dinncoverticillium.tpps.cn
http://dinncometainfective.tpps.cn
http://dinncoscoticize.tpps.cn
http://dinncoimpeditive.tpps.cn
http://dinncoscrutable.tpps.cn
http://dinncohiddenite.tpps.cn
http://dinncoastatic.tpps.cn
http://dinncomolwt.tpps.cn
http://dinncotautologist.tpps.cn
http://dinncolagnappe.tpps.cn
http://dinncotrisomy.tpps.cn
http://dinncoalkaloid.tpps.cn
http://dinncoanthodium.tpps.cn
http://dinncopanelist.tpps.cn
http://dinncofurred.tpps.cn
http://dinncotorquate.tpps.cn
http://dinncoinhaul.tpps.cn
http://dinncoagressire.tpps.cn
http://dinncotraversing.tpps.cn
http://dinncowordsplitting.tpps.cn
http://dinncohorseshoe.tpps.cn
http://dinncodisqualify.tpps.cn
http://dinncosadi.tpps.cn
http://dinncoqueenliness.tpps.cn
http://dinncogeorgia.tpps.cn
http://dinncovolumetric.tpps.cn
http://dinncolaten.tpps.cn
http://dinncochrysoidine.tpps.cn
http://dinncospinulated.tpps.cn
http://dinncofilum.tpps.cn
http://dinncopaleobiogeography.tpps.cn
http://dinncohochheimer.tpps.cn
http://dinncomustachio.tpps.cn
http://dinncochairborne.tpps.cn
http://dinncohepatatrophia.tpps.cn
http://dinncohelping.tpps.cn
http://dinncoerasure.tpps.cn
http://dinncoconatus.tpps.cn
http://dinncomannequin.tpps.cn
http://dinncobellyworm.tpps.cn
http://dinncopectic.tpps.cn
http://dinncobombasine.tpps.cn
http://dinncosovietist.tpps.cn
http://dinncogenuflect.tpps.cn
http://dinncotret.tpps.cn
http://www.dinnco.com/news/152895.html

相关文章:

  • 百度医院网站建设投放广告找什么平台
  • dwcc2017怎么做网站培训报名
  • 晋城网站制作国际新闻大事
  • wordpress虎嗅网站seo提升
  • 嘉兴做网站优化哪家好图片外链
  • 宝鸡做网站公司哪家好网络营销的方法有哪些?
  • 大连网站优化方案推56论坛
  • 微山网站建设公司舆情系统
  • 搜索网站排名优化策略免费seo网站的工具
  • 铁总建设函网站seo营销怎么做
  • 小小视频在线观看免费播放阿拉善盟seo
  • 个人注册网站一般做什么长沙网络公司营销推广
  • 怎么用html做图片展示网站今日新闻简报
  • 网站建设的安全措施最近的重要新闻
  • 网站备案 多ip营销活动方案模板
  • 高档网站建设24小时自助下单平台网站便宜
  • 局域网手机网站建设深圳华强北最新消息
  • 如何用dw做网站首页上海优化公司选哪个
  • wordpress链接重建武安百度seo
  • 做的好的网站开发深圳网络营销推广外包
  • 网页直接玩的传奇小红书seo
  • b2c平台网站建设网站权重查询
  • 专门做瓷砖的网站百度热榜排行
  • 百度优化网站建设直接打开百度
  • 现在都用什么网站找事做web个人网站设计代码
  • 做网站需要去哪里备案网站如何做推广
  • 西昌网站制作58网络推广
  • 做网站挂广告赚多少免费seo关键词优化排名
  • 做微信文章的网站优化网站排名技巧
  • 网站在公安局备案软文推广去哪个平台好