问题描述:

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.

算法分析:观察下就可以发现被水填满后的形状是先升后降的塔形,因此,先遍历一遍找到塔顶,然后分别从两边开始,往塔顶所在位置遍历,水位只会增高不会减小,且一直和最近遇到的最大高度持平,这样知道了实时水位,就可以边遍历边计算面积。

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

TrappingRainWater的更多相关文章

  1. leetcode — trapping-rain-water

    /** * Source : https://oj.leetcode.com/problems/trapping-rain-water/ * * Created by lverpeng on 2017 ...

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

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

  3. leetcode算法分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  4. BUG-FREE-For Dream

    一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...

  5. 转载 ACM训练计划

    leetcode代码 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode. ...

  6. LeetCode题目分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  7. [LeetCode]题解(python):042-Trapping Rain Water

    题目来源 https://leetcode.com/problems/trapping-rain-water/ Given n non-negative integers representing a ...

  8. Trapping Rain Water [LeetCode]

    Problem Description: http://oj.leetcode.com/problems/trapping-rain-water/ Basic idea: Get the index ...

  9. <转>LeetCode 题目总结/分类

    原链接:http://blog.csdn.net/yangliuy/article/details/44514495 注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http:/ ...

随机推荐

  1. 《从零开始学Swift》学习笔记(Day 61)——Core Foundation框架之内存管理

    原创文章,欢迎转载.转载请注明:关东升的博客 在Swift原生数据类型.Foundation框架数据类型和Core Foundation框架数据类型之间转换过程中,虽然是大部分是可以零开销桥接,零开销 ...

  2. Pycharm创建Django admin用户名和密码

    1.Tools>Run manage.py Task 2.依次输入: makemigrations migrate createsuperuser 如: manage.py@production ...

  3. window.location.href = window.location.href window.location.reload()

    w 0-会议预订提交了预订日期,预订成功后默认显示仅显示当前日期的新页面若显示预定日的信息,则可以对预定日存入cookie: http://stackoverflow.com/questions/24 ...

  4. ssh login waiting too much time

    usually dns error, please check /etc/resolv.conf

  5. Cisco路由器DHCP配置浅析

    enable  config terminal (进入配置模式)  ip dhcp pool global(配置一个根地址池,global是地址池的名称,你可以采用有意义的字符串来表示) config ...

  6. 异常处理、socke基于TCP协议编程

    一.异常处理 1.错误和异常 1.程序中难免出现错误,而错误分成两种 (1)语法错误(这种错误过不了Python解释器的语法检测,必须在程序执行前改正) #语法错误示范一 if #语法错误示范二 de ...

  7. What is tail-recursion

    Consider a simple function that adds the first N integers. (e.g. sum(5) = 1 + 2 + 3 + 4 + 5 = 15). H ...

  8. testng xml配置文件

    简单介绍 运行TestNG测试脚本有两种方式:一种是直接通过IDE运行(例如使用eclipse中的“Run TestNG tests”),另一种是从命令行运行(通过使用xml配置文件).当我们想执行某 ...

  9. ServiceModel 元数据实用工具 (Svcutil.exe)

    ServiceModel 元数据实用工具用于依据元数据文档生成服务模型代码,以及依据服务模型代码生成元数据文档 一.SvcUtil.exe ServiceModel 元数据实用工具可在 Windows ...

  10. js高级---js架构

    ECMAScript1997 年欧洲计算机制造商协会 39 号技术委员会制定了ECMA-262标准(别名 ECMAScript),而浏览器只是负责实现,ie浏览器实现的结果是jscript,远景浏览器 ...