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.

 class Solution {
public int trap(int[] height) {
int left = 0;
int right = height.length-1;
int trap = 0;
int h = 0;
while(left<right){
if(height[left]<height[right]){
h = Math.max(height[left],h);
trap+=(h - height[left]);
left++;
}
else{
h = Math.max(height[right],h);
trap+=(h - height[right]);
right--; }
}
return trap;
}
}

42. Trapping Rain Water(直方图 存水量 hard)的更多相关文章

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

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

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

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

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

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

  4. leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II

    11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...

  5. 刷题42. Trapping Rain Water

    一.题目说明 题目是42. Trapping Rain Water,翻译起来就是"接雨水".给n个非负正数代表高度,每个正数宽度为1,让计算能多少雨水.题目难度是Hard 二.我的 ...

  6. LeetCode - 42. Trapping Rain Water

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

  7. [Leetcode][Python]42: Trapping Rain Water

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...

  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. 【LeetCode】42. Trapping Rain Water

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

随机推荐

  1. 【基础练习】【BFS+A*】codevs1225八数码难题题解

    题目描写叙述 Description Yours和zero在研究A*启示式算法.拿到一道经典的A*问题,可是他们不会做,请你帮他们. 问题描写叙述 在3×3的棋盘上,摆有八个棋子,每一个棋子上标有1至 ...

  2. 『Spring.NET+NHibernate+泛型』框架搭建之DAO(三)★

    本节内容介绍Nhibernate所封装的数据库訪问层.只是我增加了泛型进行封装.大概思路:首先,我们有一个接口层,另一个相应的实现层.在接口层中我们先定义一个父接口,父接口中定义每个接口都可能会用到的 ...

  3. 使用 Estimator 构建卷积神经网络

    来源于:https://tensorflow.google.cn/tutorials/estimators/cnn 强烈建议前往学习 tf.layers 模块提供一个可用于轻松构建神经网络的高级 AP ...

  4. Rename a local and remote branch in git

    If you have named a branch incorrectly AND pushed this to the remote repository follow these steps b ...

  5. js函数柯里化

    function curry(fn){ // 代码 } function add(a,b,c){ return a + b + c; } const execAdd = curry(add); exe ...

  6. iOS开发之--iOS APP打包的时候出现的四个选项

  7. MD5骨骼动画模型加载

    前面我们分析了静态模型OBJ格式,桢动画模型MD2,这篇主要分析骨骼动画MD5的一些概念并且实现. 混合桢动画有计算简单,容易实现等优点,但是在需要比较细致的效果时,则需要更多的关键桢,每桢都添加相同 ...

  8. UIWindow和UIScreen

    UIWindow和UIScreen 目录 概述 职责 实用操作 概述 UIWindow职责 包含了应用程序的可视化的内容 为视图和其他应用程序对象在触摸事件中提供了关键性的作用 与视图控制器一起协作来 ...

  9. Hibernate如何执行存储过程?

    Hibernate如何执行存储过程? @Overridepublic Boolean setVarValue(final String processInstanceId, final String ...

  10. 160525、高并发之mysql主从复制(linux)

    第一步:新建两台linux主机(我这里是使用虚拟机,linux版本是CentOS-6.3-x86_64-bin-DVD1.iso,注意:我下载的是dvd1,如果其他版本默认安装可能会自动还原) 第二步 ...