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. DbHelper.ttinclude 更新,查询视图和表

    <#+ public class DbHelper { #region GetDbTables public static List<DbTable> GetDbTables(str ...

  2. RTC教程

    Tutorial: Get started with Rational Team Concert Getting Started with Jazz Source Control RTC入门教程及冲突 ...

  3. php扩展模块安装

     

  4. IP冲突解决方案

    客人在我所供职的酒店上网的时候,经常会弹出一个对话框,显示一些提示,如上网的注意事项和消费标准等信息;并且有自己的电影和歌曲服务器,DHCP-server也是其中的一台服务器,宾馆.酒店就是用这台机器 ...

  5. libaio.so.1: undefined reference to `__stack_chk_fail@GLIBC_2.4'

    没有别的原因: 找正确的  libaio.so.1 包就成. 我这儿有,需要的可以下载奥!

  6. Eclipse中项目全部报错----项目全部打红叉的解决办法

    今天遇到一个超级郁闷的事情,Eclipse新建的项目全部都打有红叉,我起初以为自 己可能是因为这两天一直在配置NDK开发环境方面的东西,是不是一不小心把那个地方给配置了,然后新建项目时项目都会出现红叉 ...

  7. CorelDRAW中如何分布对象

    分布对象功能主要用来控制选择对象之间的距离,可以满足用户对均匀间距的要求,通常用于选择三个或三个以上的物体,将他们之间的距离平均分布.本教程将详解CorelDRAW中关于分布对象的操作. CorelD ...

  8. 详解MathType中如何更改公式颜色

    在MathType数学公式编辑器中可以通过更改公式颜色,起到美化.标注公式的效果.本教程将详解MathType中如何更改公式颜色. 点击菜单栏中的样式->格式->颜色,用户就可以根据自己的 ...

  9. 如何分析解决Android ANR(转载)

    转载自:http://blog.csdn.net/dadoneo/article/details/8270107 一:什么是ANR ANR:Application Not Responding,即应用 ...

  10. 8 -- 深入使用Spring -- 3...1 Resource实现类

    8.3.1 Resource实现类 Resource接口是Spring资源访问的接口,具体的资源访问由该接口的实现类完成. Spring提供的Resource接口的实现类: ⊙ UrlResource ...