LeetCode OJ 42. 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.

The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
【题目分析】
题目给出一个整数序列代表高度图,要求计算下雨后图中的积水量。
【思路】
我们只要找到高度图中的凹陷部分,然后把所有凹陷的容积加起来即可。那么如何找到这些凹陷部分呢?
有这样一个想法:给定高度图两端的高度,那些比这两端都低的部分肯定会形成凹陷,如果是相等则不形成凹陷,如果比当前高度高,则我们要重新确定高度图的两端高度。这个过程如下:
1. 初始两端最低高度high = 0, 容量capacity = 0;

2. height[begin] <= high,不形成凹陷,begin++;

3. height[begin] > high; height[end] > high;high = min(height[begin], height[end]); 因此high = 1;
此时height[begin] == high,height[end] == high;所以begin++,end--;

4. height[begin] < high;形成凹陷,capacity += high - height[begin];此时capacity = 1;begin++;

5. height[begin] > high; height[end] > high;high = min(height[begin], height[end]); 因此high = 2;
此时height[begin] == high,height[end] == high;所以begin++,end--;

6. height[begin] < high;形成凹陷,capacity += high - height[begin];此时capacity = 2;begin++;
height[end] < high;形成凹陷,capacity += high - height[begin];此时capacity = 3;end--;

7. height[begin] < high;形成凹陷,capacity += high - height[begin];此时capacity = 5;begin++;
height[end] = high;不形成凹陷;end--;

7. height[begin] < high;形成凹陷,capacity += high - height[begin];此时capacity = 6;begin++;

8. 返回最大容量6;
【java代码】
public class Solution {
public int trap(int[] height) {
if(height == null || height.length <= 2) return 0;
int sum = 0, maxhigh = 0;
int begin = 0, end = height.length-1;
while(begin <= end){
if(height[begin] < maxhigh){
sum += maxhigh - height[begin++];
}
else if(height[end] < maxhigh){
sum += maxhigh - height[end--];
}
else{
maxhigh = Math.min(height[begin], height[end]);
if(height[begin] <= maxhigh) begin++;
if(height[end] <= maxhigh) end--;
}
}
return sum;
}
}

LeetCode OJ 42. Trapping Rain Water的更多相关文章
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
- 【LeetCode】42. Trapping Rain Water
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
- leetcode problem 42 -- Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 【一天一道LeetCode】#42. Trapping Rain Water
一天一道LeetCode系列 (一)题目 Given n non-negative integers representing an elevation map where the width of ...
- 【LeetCode】42. Trapping Rain Water 接雨水 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 保存左右最大值 单调栈 日期 题目地址:ht ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
- [array] leetcode - 42. Trapping Rain Water - Hard
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
随机推荐
- 问题记录2:TypeError: write() argument must be str, not bytes
今天试了下用requests模块的get()方法来下载图片,写入文件的时候不能写入二进制,然后将打开方式改成二进制的就好了. 原因是,f.content的存储方式是二进制,而文件正常打开默认是字符串的 ...
- js函数的可变参数
//对于js的可变参数的清空,在定义函数式不需要写上参数, 在函数内部使用argument对象可以 直接获取参数个数等信息 //在调用函数式可以传递任意个数的参数 function text(){ v ...
- ACM第五次积分赛
做出三道题,第二名,总积分上升到第八名,继续加油! SAU-ACM总比赛成绩 姓名 账号 上学期成绩 第一次成绩 第二次成绩 第三次成绩 第四次成绩 第五次成绩 总成绩 张国庆 143401 ...
- android 控件自定义样式
一.按钮(Button) 方式1.存在.9图片或图片时 可在drawable文件夹下新建xml文件style_button_one.xml,代码如下 <?xml version=" ...
- 图片添加border 不占用图片的大小
因为设计稿中的分割线大多分为两种情况:1.在图片右侧:2.在图片右侧+下方. 那么使用伪类before和after以及绝对定位很容易在不改变原布局的情况使图片按照设计稿输出和保留分割线. 例div.i ...
- css-ie6下实现最小,最大宽度
_width: expression((document.documentElement.clientWidth||document.body.clientWidth)<1040?"1 ...
- Xsser
来源:https://www.cqhacker.cn/post-174.html XSSer使用说明 =============================================== ...
- 【Python】迭代器
对迭代器和生成器的概念一直很混乱,总结一下: 迭代器: 1.所谓的迭代器,就是具有__next__()方法的对象: 2.__iter__()方法返回一个迭代器对象,这个对象必须具有__next__() ...
- dev Gridcontrol单元格值格式化及在模板中调用命令
<dxg:GridColumn> <dxg:GridColumn.EditSettings> ...
- string 数字序列大小比较
string 数字序列大小比较 string.compare string a = "022"; string b="1"; 比较结果 '022' < ' ...