LeetCode-Trapping Rain Water 又是这种需要仔细考虑的题目, 很容易就会漏掉一些情况,哎,感冒了, 完全不在状态,调了好几个小时, 写代码的能力实在是不行,总感觉写出来的代码不够优雅, 1classSolution {2public:3inttrap(intA[],intn) {4//Start typing your C/C++ solution below
Trapping Rain Water 题解 题目描述 即现实中的地面积水问题。抽象出来即从某个序列Seq中提取出先单调递减后单调递增的子序列集合(集合中子序列只允许头尾存在重复),求此集合中所有子序列s(长为d)的"凹陷值"((d - 2) * min{s[0],s[d-1]} - (sum(s) - s[0] - s[d-1]))之和。如:[0,1,0...
1. 题目 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. The above elevation map is represented by array [0,1,0,...
result +=max(0,min(h, maxh[i]) - height[i])returnresultif__name__ =="__main__":assertSolution().trap([0,1,0,2,1,0,1,3,2,1,2,1]) ==6 欢迎查看我的Github(https://github.com/gavinfish/LeetCode-Python) 来获得相关源代码。
LeetCode刷题笔记--11. Container With Most Water 也找不出来反例证明这个算法不对。 class Solution { public: int maxArea(vector<;int>;& height) { int i=0; int...我用暴力法来做,结果超时了。 暴力法的代码: class Solution { public: int maxArea(vector<;int>;& height) { int ...
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 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:
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」; ...
publicclassSolution{/** * @param heights: a list of integers * @return: a integer */publicinttrapRainWater(int[] heights){// write your code hereintwater =0; Deque<Integer>queue=newArrayDeque<>();for(inti =0; i < heights.length; i++) {while(!queue.isEmpty() && heights[i] > ...