Level:

  Hard

题目描述:

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.



The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!

Example:

Input: [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6

思路分析:

  设置两个指针left和right,分别指向数组的首尾位置,然后从两边向中间扫描,在当前两指针确定的范围内,先比较两头找出较小值,如果较小值是left指向的值,那么就从左向右扫描,如果较小的值是right指向的值,则从右向左扫描,若遇到的值比当前较小值小,则将差值存入结果,如果遇到的值大,则重新确定新的窗口范围,以此类推直到left和right指针重合。

代码:

public class Solution{
public int trap(int[]height){
int left=0;
int right=height.length-1;
if(height==null||height.length==0)
return 0;
int res=0;
while(left<right){
int min=Math.min(height[left],height[right]); //找到两端较小的值
if(min==height[left]){
++left;
while(left<right&&height[left]<min){
res=res+min-height[left];
left++;
}
}else{
--right;
while(left<right&&height[right]<min){
res=res+min-height[right];
right--;
}
}
}
return res;
}
}

70.Trapping Rain Water(容水量)的更多相关文章

  1. Trapping Rain Water I && II

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

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

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

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

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

  4. [LintCode] Trapping Rain Water 收集雨水

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

  5. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

  6. LeetCode - 42. Trapping Rain Water

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

  7. 有意思的数学题:Trapping Rain Water

    LeetCode传送门 https://leetcode.com/problems/trapping-rain-water/ 目标:找出积木能容纳的水的“面积”,如图中黑色部分是积木,蓝色为可容纳水的 ...

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

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

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

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

随机推荐

  1. tomcat日志切割脚本shell

    tomcat-rotate.sh: #!/bin/bash log_path="/home/tomcat7-api/logs/"expried_time=7 function de ...

  2. Jupyter Notebook 安装与使用

    Ref: https://jupyter.org/install Installing Jupyter Notebook with pip python -m pip install --upgrad ...

  3. xml转dict

    xml转dict 最开始的时候一直是按格式比较严谨的XML格式进行的转换,所以一般只需要考虑两种情况就可以了,即各个节点或者子节点全相同或者全不同,全相同按list处理,全不同按dict处理,这么一想 ...

  4. lsattr 查看文件扩展属性

    1. 命令功能 lsattr查看 是否有chattr设置的权限. 2. 使用范例 [root@localhost data]# lsattr resolv.conf -----a-------e- r ...

  5. keras 下载预训练模型报错SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)

    import ssl ssl._create_default_https_context = ssl._create_unverified_context https://stackoverflow. ...

  6. 专家告诉你!如何避免黑客BGP劫持?

    BGP前缀劫持是针对Internet组织的持久威胁,原因是域间路由系统缺乏授权和身份验证机制. 仅在2017年,数千起路由事件导致代价高昂的中断和信息拦截,而问题的确切程度未知.尽管在过去20年中已经 ...

  7. YOLOV3算法详解

     YOLOV3 YOLO3主要的改进有:调整了网络结构:利用多尺度特征进行对象检测:对象分类用Logistic取代了softmax. 新的网络结构Darknet -53 darknet-53借用了re ...

  8. Java+Maven的工程运行Sonar的方式

    step 1:在maven->setting.xml中进行配置 修改mvn工程所用的setting.xml文件,在<profiles></profiles>节点中增加: ...

  9. windows系统如何查看物理cpu核数,内存型号等

    首先,我们需要打开命令行模式,利用win+r键打开运行,输入cmd回车即会出现   然后在命令行界面输入wmic进入命令行系统管理执行脚本界面     然后我们通过cpu get *可以查看cpu的具 ...

  10. redux简单使用

    在前面的随便中有简单的使用过redux和react-redux,但是感觉写在一起,总是理不清楚,后面看了技术胖老师关于redux的视频后,感觉自己又有了新的理解,在这里简单记录一下. 项目准备 首先安 ...