问题描述:

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.

算法分析:观察下就可以发现被水填满后的形状是先升后降的塔形,因此,先遍历一遍找到塔顶,然后分别从两边开始,往塔顶所在位置遍历,水位只会增高不会减小,且一直和最近遇到的最大高度持平,这样知道了实时水位,就可以边遍历边计算面积。

public class TrapingRainWater
{
public int trap(int[] height)
{
int n = height.length;
if(n <= 2)
{
return 0;
}
int max = -1;
int maxIndex = 0;
for(int i = 0; i < n; i ++)
{
if(height[i] > max)
{
max = height[i];
maxIndex = i;
}
}
int area = 0;
int root = height[0];
for(int i = 0; i < maxIndex; i ++)
{
if(root < height[i])
{
root = height[i];
}
else
{
area += (root - height[i]);
}
}
root = height[n-1];
for(int i = n-1; i > maxIndex; i --)
{
if(root < height[i])
{
root = height[i];
}
else
{
area += (root - height[i]);
}
}
return area;
} }

TrappingRainWater的更多相关文章

  1. leetcode — trapping-rain-water

    /** * Source : https://oj.leetcode.com/problems/trapping-rain-water/ * * Created by lverpeng on 2017 ...

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

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

  3. leetcode算法分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  4. BUG-FREE-For Dream

    一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...

  5. 转载 ACM训练计划

    leetcode代码 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode. ...

  6. LeetCode题目分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  7. [LeetCode]题解(python):042-Trapping Rain Water

    题目来源 https://leetcode.com/problems/trapping-rain-water/ Given n non-negative integers representing a ...

  8. Trapping Rain Water [LeetCode]

    Problem Description: http://oj.leetcode.com/problems/trapping-rain-water/ Basic idea: Get the index ...

  9. <转>LeetCode 题目总结/分类

    原链接:http://blog.csdn.net/yangliuy/article/details/44514495 注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http:/ ...

随机推荐

  1. 与Mysqli相关的四种数据库取值

    <!--取值方案一:通过数字数组 fetch_row()--><meta http-equiv="Content-Type" content="text ...

  2. Refused to execute script from '....js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.md

    目录 问题描述 解决过程 总结 问题描述 在整合 Spring Boot.Spring Security.Thymeleaf 的练习中,对页面进行调试时,发现如下错误提示: Refused to ex ...

  3. Grunt-Less批量编译为css

    Grunt批量编译less module.exports = function (grunt) { grunt.initConfig({ less: { main: { expand: true, s ...

  4. threading模块、ThreadLocal

    一.threading模块 1.线程对象的创建 1.1 Thread类直接创建 import threading import time def countNum(n): # 定义某个线程要运行的函数 ...

  5. Leetcode注意

    List<List<Integer>> res = new ArrayList<>();

  6. 使用Ehcache缓存同步启动时抛出异常net.sf.ehcache.CacheException: Can't assign requested address

    这个问题在插入公司内网网线的时候不会复现,由于我使用的是公司无线网络,故导致此问题. 具体解决办法是:在启动服务时,指定使用默认ipv4的网络接口.可以在启动jvm时添加参数-Djava.net.pr ...

  7. Library Cache优化与SQL游标

    Library Cache主要用于存放SQL游标,而SQL游标最大化共享是Library Cache优化的重要途径,可以使SQL运行开销最低.性能最优. 1 SQL语句与父游标及子游标 在PL/SQL ...

  8. 动态切换数据库(EF框架)

             文章简略:本文测试项目为Silverlight+EF+RIA Service动态切换数据库的问题 通常,Ado.net EntityFramework的数据库连接字符串Connect ...

  9. PHP iconv函数

    最近在做一个程序,需要用到iconv函数把抓取来过的utf-8编码的页面转成gb2312, 发现只有用iconv函数把抓取过来的数据一转码数据就会无缘无故的少一些. iconv函数库能够完成各种字符集 ...

  10. LeetCode 1. Two Sum 找到两数字求和索引集合

    https://leetcode.com/problems/two-sum/description/ 第一种方法  遍历查找 // // main.m // HFCDemo // // Created ...