Problem: 根据所给数组的值,按照上图的示意图。求解积水最大体积。

 
 
首先对所给数组进行遍历操作,求出最大高度所对应的下标为maxIndex
 
之后从左向右进行遍历,设置左边高度为leftMax 并初始化为 height[0],从i==1到i==maxIndex进行遍历。不断更新water,以及leftMax
    当height[I]小于leftMax时,water += leftMax - height
    当height[i]大于leftMax时,leftMax = height[I]
 
之后从i==height.length - 2到下标 I == maxIndex进行遍历操作 初始化rightMax = height[height.length-1]
    当height[I]小于rightMax时,water+=rightMax - height[I]
    当height[I]大于rightMax时,rightMax = height[I]
 
函数结果返回water
 
参考代码:
 
package leetcode_50;

/***
*
* @author pengfei_zheng
* 求积水的体积
*/
public class Solution42 {
public static int trap(int[] height) {
if (height.length <= 2) return 0;
int max = -1;
int maxIndex = 0;
for (int i = 0; i < height.length; i++) {
if (height[i] > max) {
max = height[i];
maxIndex = i;
}
} int leftMax = height[0];
int water = 0;
for (int i = 1; i < maxIndex; i++) {
if (height[i] > leftMax) {
leftMax = height[i];
} else {
water += leftMax - height[i];
}
} int rightMax = height[height.length - 1];
for (int i= height.length - 2; i > maxIndex; i--) {
if (height[i] > rightMax) {
rightMax = height[i];
} else {
water += rightMax - height[i];
}
} return water;
}
public static void main(String[]args){
// int []height={0,1,0,2,1,0,1,3,2,1,2,1};
int []height={10,0,11,0,10};
System.out.println(trap(height));
}
}

LeetCode 42 Trapping Rain Water(积水体积)的更多相关文章

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

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

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

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

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

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

  4. LeetCode - 42. Trapping Rain Water

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

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

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

  6. leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

  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] 42. Trapping Rain Water 解题思路

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

  9. Java [Leetcode 42]Trapping Rain Water

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

随机推荐

  1. JavaScript中String和JSON互换

    最简答的方式是: JSON.parse(obj) 将json对象解析为json字符串 JSON.stringify(str) 将json字符串转为json对象. 需要注意的是早期的IE浏览器是没有JS ...

  2. 深入探讨:MySQL数据库MyISAM与InnoDB存储引擎的比较

    From: http://www.jb51.net/article/37766.htm MySQL有多种存储引擎,MyISAM和InnoDB是其中常用的两种.这里介绍关于这两种引擎的一些基本概念(非深 ...

  3. 精美的HTML5/CSS3表单 带小图标

    今天我们要来分享一款非常精美的HTML5/CSS3表单,准备地说,这是一款经过美化的input输入表单,每一个输入表单都可以定义其两侧的小图标,非常华丽.另外,这款表单应用还采用了3种不同的风格主题, ...

  4. 关于最近WIN7系统错误711的解决办法

    昨天晚上有发现部分用户反馈错误711,因为不在现场不清楚是怎么一回事,今天早上又有其他客户反馈他下面有4个用户发生711错误. 刚好在电脑边上,就拨号试下,结果我的也是711 这个711的症状是: 单 ...

  5. Scala学习笔记——安装

    安装scala,不要使用sudo apt-get install scala来安装 1.从下面网址来下载Scala文件 http://www.scala-lang.org/download/2.11. ...

  6. objelement = event.target || event.srcElement;

    objelement = event.target || event.srcElement; function updateProductVideo(e){ e = window.event || a ...

  7. 安装yeoman报没有权限的错误

    新的ubuntu服务器, 不小心先装了npm, 再装的node, 再用meanjs装的yeoman(即不是自己npm install -g yo装的, 是用meanjs的stack一步到位的),而正常 ...

  8. JS中 try...catch...finally (转)

    JS的try..catch..finally var array = null; try { document.write(array[0]); } catch(err) { document.wri ...

  9. mysql中json_object函数的使用?

    需求说明: 今天看了json_object函数的使用,在此记录下使用过程 操作过程: 1.使用json_object函数将一个键值对列表转换成json对象 mysql> select json_ ...

  10. c++运算符重载---20

    原创博文,转载请标明出处--周学伟 http://www.cnblogs.com/zxouxuewei/ c++的一大特性就是重载(overload),通过重载可以把功能相似的几个函数合为一个,使得程 ...