代码 classSolution{publicinttrap(int[] height){intresult=0;if(height.length ==0)returnresult;int[] left_height_array =newint[height.length];int[] right_height_array =newint[height.length];intleft_max=0;intright_
https://leetcode-cn.com/problems/trapping-rain-water/ 题目内容 给定n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。 示例: 输入: [0,1,0,2,1,0,1,3,2,1,2,1] 输出: 6 思路 我们可以设置两个数组,「left_height_array」和「right_height_array」; 其...
https://leetcode.cn/problems/trapping-rain-water/description/ 给定n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。 这道题如果难的话就是难在思路,怎么去算 三、终版 o(n) 找到最高点,两边依次以最高边为终边,看自己是否有积水,是否有积水取决于历史是否有高...
https://leetcode.com/problems/trapping-rain-water/discuss/17414/A-stack-based-solution-for-reference-inspired-by-Histogram https://leetcode.com/problems/trapping-rain-water/discuss/17357/Sharing-my-simple-c%2B%2B-code%3A-O(n)-time-O(1)-space LeetCode All in One 题目讲解汇总(持续更新中.....
[Leetcode] Trapping Rain Water 积水问题 Trapping Rain Water Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6....
https://leetcode-cn.com/problems/trapping-rain-water/ 题目内容 给定n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。 示例图 示例: 输入: [0,1,0,2,1,0,1,3,2,1,2,1] 输出: 6 思路 我们可以设置两个数组,「left_height_array」和「right_height_array...
LeetCode 42. Trapping Rain Water(Hard) image! Example: Solution 该题很有意思,可以当作是在砌墙:输入一个数组,数组元素可以当作当前位置墙的高度,问该种情况,能装多少水(可以这么理解,不过实际上是2维平面,只考虑面积...较少的那一个。 先分享一个错误的想法: 可以通过一次遍历找到所有可以“装水”的gap...
## LeetCode 42H - Trapping Rain Water,双指针法 from typing import List class Solution: ## height 表示列表中元素的取值,高度 def trap(self, height: List[int]) -> int: if not height: ## 如果没有任何高度 return 0 ## 接不住任何雨水 ## 初始化左右指针 l, r = 0, len(height) - 1...
42. Trapping Rain Water 这道题看了LeetCode的Solution里的动态图,很好理解: 只要找到最大的elevation,那么左右任何一个elevation到最大的elevation必能积水(前提减去两者之间的elevation),还是不懂的可以多看看Solution里的动态图 class Solution { public:
class Solution: """ @param heights: a list of integers @return: a integer """ def trapRainWater(self, heights): if not heights: return 0 left_max = [] curt_max = -sys.maxsize for height in heights: curt_max = max(curt_max, height) left_max.append(curt_max) right_max = []...