给定 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. jquery操作全选、批量删除、加减行

    --------------------------------------------------------------------------------------- html静态页面 --- ...

  2. str_2.判断两个字符串是否互为旋转词

    1. 字符串str的前面任意部分挪到后面形成的字符串叫做字符串str的旋转词 $str1 = "2ab1"; $str2 = "ab12"; $ret = is ...

  3. TestDescription文档描述测试过程

    测试描述文档是用xml语言描述测试过程的文档,一个测试过程包括测试信号建立,UUT引脚确定,建立连接关系,数据测量,断开连接关系,复位测试信号等步骤. 下图用标准的ATML语言描述了接通直流电源并测量 ...

  4. ASCII UNICODE UTF "口水文"

    最近接了一个单是需要把非 UTF-8 (No BOM)编码的文件转换成 UTF-8 (No BOM),若此文件是 UTF-8 但带有 BOM ,需要转换成不带 BOM 的.于是开启了一天的阅读.首先花 ...

  5. css 3d box 实现的一些注意事项

    Test1.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  6. 用rem适配移动端

    常见方式: 1. 固定宽度(320)做法:这样前端倒是爽了,可是大页面两边有留白,小页面图标文字又会缩的很小,用户体验极其不好. 2. 流式布局:其实就是用%,这样宽度倒还差不多,高度怎么搞?所以这种 ...

  7. MQTT协议简介及协议原理

    MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议),是一种基于发布/订阅(publish/subscribe)模式的“轻量级”通讯协议,该协议构建 ...

  8. Windows WMIC命令使用详解1

    https://blog.csdn.net/enweitech/article/details/51982114 在CMD和Powershell中 使用WMIC 先决条件: a. 启动Windows ...

  9. 系统启动挂载根文件系统时Kernel panic

    转自:http://qiuye.iteye.com/blog/543595 这类问题很常见,先总体介绍一下解决思路. 能出现让人激动的的控制台,那么系统移植已经接近完成:但是不少人在最后一步出现问题. ...

  10. jsp九大内置对象与servlet中java对象

    jsp九大内置对象 request对象 :  getParameter(String name)获取表单提交的数据 getParamegerNames() 获取客户端提交的所有参数名 getAttri ...