问题描述:

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. CodeForces 156A Message(暴力)

    A. Message time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  2. react 坑总结

    1.react可以在里面直接更改state的变量 例如: 2.react 数组循环

  3. delphi ,安装插件

    一.安装 DevExpress52(VCL+Demo+Help+汉化ini)cxGrid技巧:DBGRIDEH 安装:点击devcl52.exe,安装,安装路径到D:\Program Files\Co ...

  4. Powershell About Active Directory Server

    一.获取域控制器服务器清单 (Get-ADForest).Domains | %{ Get-ADDomainController -Filter * -Server $_ } | select hos ...

  5. php 汉字验证码

    代码: captcha.php <?php //实现简单的验证码 //session_start session_start(); //画布 $image = imagecreatetrueco ...

  6. pycharm-->github / github-->pycharm

    一.pycharm -->发布到--> github:1.github 账号密码 https://github.com/2.git 下载安装 https://git-scm.com/3.配 ...

  7. shell判断文件/目录是否存在

    https://www.cnblogs.com/37yan/p/6962563.html caution!!! if should be end with fi caution!!! there sh ...

  8. Drawable.Callback

     一.介绍 public abstract void invalidateDrawable (Drawable who) Called when the drawable needs to be re ...

  9. PHP获取客户端的IP

    function getClientIP(){    global $ip;    if (getenv("HTTP_CLIENT_IP"))        $ip = geten ...

  10. Linux基础整理 + 注释

    1.Linux的常用命令: ls 显示当前文件夹目录 ll -->详细信息 ls -a 显示所有文件 ls -lhSr w 查看登录的帐号,还可以查看cpu负载情况,who am i ,who ...