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.

解题思路:

先找到第一块最高的木板,然后从前向最高木板遍历,接着从后向最高木板遍历,JAVA实现如下:

    static public int trap(int[] height) {
int result=0,peak=0,firstMax=0;
for(int i=0;i<height.length;i++)
if(height[i]>height[firstMax])
firstMax=i;
for(int i=0;i<firstMax;i++)
if(height[i]>height[peak])
peak=i;
else result+=height[peak]-height[i];
peak=height.length-1;
for(int i=height.length-1;i>firstMax;i--)
if(height[i]>height[peak])
peak=i;
else result+=height[peak]-height[i];
return result;
}

Java for LeetCode 042 Trapping Rain Water的更多相关文章

  1. LeetCode 042 Trapping Rain Water

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

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

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

  3. [LeetCode] 407. Trapping Rain Water II 收集雨水 II

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  4. leetcode#42 Trapping rain water的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...

  5. [array] leetcode - 42. Trapping Rain Water - Hard

    leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...

  6. LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))

    LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...

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

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

  8. [LeetCode] 407. Trapping Rain Water II 收集雨水之二

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  9. LeetCode - 42. Trapping Rain Water

    42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...

随机推荐

  1. myeclipse 配置weblogic

    1.打开myeclipse,选择Window -> Preferences--->MyEclipse--->servers 2.点击servers---->weblogic-- ...

  2. CruiseControl.NET开篇

    在这里说明一下,我终于踏上了CruiseControl.NET这条不归路了,为什么我会觉得是一条不归路,原因很简单,就是这东西在现在这个阶段已经很久没有在园子里有活跃度了,基本上到了已经可以到了让大家 ...

  3. [NOIP2011] 提高组 洛谷P1312 Mayan游戏

    题目描述 Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个 7 行5 列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上.游戏通关是指在规定 ...

  4. 洛谷P2279 [HNOI2003]消防局的设立

    题目描述 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来连接这些基地,并且每两个基地都能够通过道路到达,所以所有的基地形成了一个巨大的树状 ...

  5. 洛谷P1908 求逆序对 [归并排序]

    题目描述 猫猫TOM和小老鼠JERRY最近又较量上了,但是毕竟都是成年人,他们已经不喜欢再玩那种你追我赶的游 戏,现在他们喜欢玩统计.最近,TOM老猫查阅到一个人类称之为“逆序对”的东西,这东西是这样 ...

  6. 最小圆覆盖(Smallest Enclosing Discs)

    随机增量算法(a randomized incremental algorithm) #define sqr(x) ((x)*(x)) #define EPS 1e-4 struct P{ doubl ...

  7. Swift开发学习-01 Swift介绍

    本教程是笔者在自学IOS/Swift知识的总结,适用于通过对Objictive C编程的运用并需要了解基于iOS程序的iPhone和iPad的程序员.做一个有bigger的’攻城狮‘,有尊严的工作,快 ...

  8. POJ 1789Truck History(pirme)

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22648   Accepted: 8781 De ...

  9. linux 下安装memcached与php的memcache扩展

    1. 在线安装 yum install memcached: 源代码安装 wget http://memcached.org/latest 下载最新版本 tar -zxvf memcached-1.x ...

  10. C# 设置开机自动启动(注册表方式)

    .NET技术交流群 199281001 .欢迎加入. using System; using System.Collections.Generic; using System.Linq; using ...