给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算下雨之后能接多少雨水。
例如,
输入 [0,1,0,2,1,0,1,3,2,1,2,1],返回 6。

详见:https://leetcode.com/problems/trapping-rain-water/description/

Java实现:

方法一:

先遍历一遍找到塔顶,然后分别从两边开始,往塔顶所在位置遍历,水位只会增高不会减小,且一直和最近遇到的最大高度持平,这样知道了实时水位,就可以边遍历边计算面积。

参考:https://www.cnblogs.com/felixfang/p/3713197.html

class Solution {
public int trap(int[] height) {
int n=height.length;
if(n<=2){
return 0;
}
int top=-1;
int topIndex=0;
for(int i=0;i<n;++i){
if(height[i]>top){
top=height[i];
topIndex=i;
}
}
int area=0;
int root=height[0];
for(int i=0;i<topIndex;++i){
if(root<height[i]){
root=height[i];
}else{
area+=(root-height[i]);
}
}
root=height[n-1];
for(int i=n-1;i>topIndex;--i){
if(root<height[i]){
root=height[i];
}else{
area+=(root-height[i]);
}
}
return area;
}
}

方法二:

left和right两个指针分别指向数组的首尾位置,从两边向中间扫描,在当前两指针确定的范围内,先比较两头找出较小值,如果较小值是left指向的值,则从左向右扫描,如果较小值是right指向的值,则从右向左扫描,若遇到的值比当较小值小,则将差值存入结果,如遇到的值大,则重新确定新的窗口范围,以此类推直至left和right指针重合。

class Solution {
public int trap(int[] height) {
int n=height.length;
if(n<2){
return 0;
}
int area=0;
int left=0;
int right=n-1;
while(left<right){
int min=height[left]<height[right]?height[left]:height[right];
if(height[left]==min){
++left;
while(left<right&&height[left]<min){
area+=min-height[left];
++left;
}
}else{
--right;
while(left<right&&height[right]<min){
area+=min-height[right];
--right;
}
}
}
return area;
}
}

参考:http://www.cnblogs.com/grandyang/p/4402392.html

042 Trapping Rain Water 接雨水的更多相关文章

  1. [LeetCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  2. [LeetCode] 42. Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  3. [LintCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  4. LeetCode 042 Trapping Rain Water

    题目要求:Trapping Rain Water Given n non-negative integers representing an elevation map where the width ...

  5. 【LeetCode】042 Trapping Rain Water

    题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...

  6. 【LeetCode】42. Trapping Rain Water 接雨水 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 保存左右最大值 单调栈 日期 题目地址:ht ...

  7. 【LeetCode每天一题】Trapping Rain Water(获得雨水的容量)

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  8. [leetcode][042] Trapping Rain Water (Java)

    我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步. 题目在这里: ...

  9. Java for LeetCode 042 Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

随机推荐

  1. linux命令学习笔记(44):top命令

    top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管 理器.下面详细介绍它的使用方法.top是一个动态显示过程,即可以通过用户按键来不断刷 ...

  2. MyEclipse异常关闭导致启动不了tomcat的解决方法

     由于MyEclipse的异常关闭从而导致Tomcat并没有关闭,所以再次启动Tomcat当然是无法启动的啦,解决方法:在任务管理器中关闭一个叫javaw.exe的进程,如果你这时已经启动了MyEcl ...

  3. docker基础用法

    docker 架构: docker 安装前期准备: 安装centos7 ,不要在centos6  [root@node01 yum.repos.d]# uname -a Linux node01 -. ...

  4. OpenCode:template

    ylbtech-OpenCode: 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http://ylbtec ...

  5. Java高并发(1)

    1.同步和异步的区别和联系: 所谓同步,可以理解为在执行完一个函数或方法之后,一直等待系统返回值或消息,这时程序是出于阻塞的,只有接收到 返回的值或消息后才往下执行其它的命令. 异步,执行完函数或方法 ...

  6. python3+selenium使用浏览器IE的时候,无法打开IE浏览器,老是报错: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones

    python3+selenium使用浏览器IE的时候,老是报错: Unexpected error launching Internet Explorer. Protected Mode settin ...

  7. Java对象序列化详解

    深入理解Java对象序列化 1. 什么是Java对象序列化 Java平台允许我们在内存中创建可复用的Java对象,但一般情况下,只有当JVM处于运行时,这些对象才可能存在,即,这些对象的生命周期不会比 ...

  8. 转载TCP-IP协议解释

    本文转载自 http://www.ruanyifeng.com/blog/2009/03/tcp-ip_model.html TCP/IP模型是互联网的基础, 理解 TCP/IP对理解互联网至关重要 ...

  9. IIS7启用GZip压缩

    本文转载自 http://www.cnblogs.com/kissdodog/p/6252129.html GZip压缩通常会达到70%以上的压缩率,如果是手机Web这无疑会使网站的访问速度大大增加, ...

  10. VMWare安装Ubuntu 16.04

    1.Ubuntu安装文件下载 到Ubuntu官网下载安装包,有Bit Torrent.Network installer多种方式下载. 推荐使用中国地区的镜像下载,我是用华科的校园网下载速度是非常感人 ...