题意:

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.

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

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!

题目:

给定n个非负整数来代表高度图。每一个栏的宽度为1.计算下雨之后这个东东能最多能盛多少的水。

比方,给定[0,1,0,2,1,0,1,3,2,1,2,1],
返回 6.

上图的高度图通过数组[0,1,0,2,1,0,1,3,2,1,2,1]表示出来。

这个样例中雨水(蓝色所看到的)共6个单位。

算法分析:

当刷到这个题的时候我真是醉了。这就是15年春季的阿里算法project师实习在线笔试的题目~~

一模一样,当时水笔的我真心不会做啊,笔试果断没过 囧~~

* 观察下就能够发现被水填满后的形状是先升后降的塔形。因此。先遍历一遍找到塔顶,然后分别从两边開始,往塔顶所在位置遍历,水位仅仅会增高不会减小,

* 且一直和近期遇到的最大高度持平,这样知道了实时水位。就能够边遍历边计算面积。

* 首先找到最高的,然后从左往最高处扫。

* 碰到一个数A[i]。计算A[0,,,i-1]最高的是否高过A[i]。

* 假设是。则A[i]上的水的体积为max(A[0...i-1])-A[i],否则为0而且更新最大值

AC代码:

public class Solution
{
public int trap(int[] height)
{
if(height==null||height.length==0)
return 0;
int res=0;
int maxvalue=0;
int label=0;
int startmvalue=0;
int endmvalue=0;
int mtem;
for(int i=0;i<height.length;i++)
{
if(height[i]>maxvalue)
{
maxvalue=height[i];
label=i;
}
}
startmvalue=height[0];
for(int i=0;i<label;i++)
{
if(height[i]>startmvalue) startmvalue=height[i];
else
{
res+=startmvalue-height[i];
}
}
endmvalue=height[height.length-1];
for(int i=height.length-1;i>label;i--)
{
if(height[i]>endmvalue) endmvalue=height[i];
else
{
res+=endmvalue-height[i];
}
}
return res;
}
}

[LeetCode][Java] Trapping Rain Water的更多相关文章

  1. [LeetCode] 407. Trapping Rain Water II 收集雨水 II

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

  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 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))

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

  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] 407. Trapping Rain Water II 收集雨水之二

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

  7. LeetCode - 42. Trapping Rain Water

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

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

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

  9. LeetCode 042 Trapping Rain Water

    题目要求:Trapping Rain Water Given n non-negative integers representing an elevation map where the width ...

随机推荐

  1. Angular——$http

    基本介绍 $http用于向服务端发起异步请求,同时还支持多种快捷方式如$http.get().$http.post().$http.jsonp.$hhtp也是属于内置服务的一种,这里特意提出来写一篇用 ...

  2. 简单了解了下SEO与SEM的机制

    SEO:搜索引擎优化SEM:搜索引擎营销 SEO排名机制:搜索引擎蜘蛛 权重 算法 排名规则 搜索引擎提交入口: 1.百度搜索网站登入口 2.Google网站登入口 3.360搜索引擎登入入口 4.搜 ...

  3. Python 开发初识

    从今天开始记录自己的python开发之路,用博客记录自己的学习经历,以及学习小结,小的项目模块,努力充实,做最好的自己!!!

  4. 13EL表达式语言

    EL表达式语言 EL表达式语言 JSP用于在页面上显示动态内容,通常需要在JSP页面中嵌入Java脚本以完成复杂功能.但大量的Java脚本使得JSP页面难以维护.一种类似JavaScript语言—EL ...

  5. Extjs选中多行Grid提交

    要实现的效果如图:可以选择多行grid然后提交给后台 1,Extjs中grid如何可以选择多行? 定义一个grid,将色了Type设置为多选即可 selType: 'checkboxmodel', 2 ...

  6. SIMD学习 -- 用SSE2指令作点乘和累加计算

    这几天在做学校的一个学习小项目,需要用到SIMD指令计算提速.也是第一次碰这个,看了一些资料和代码,模仿着写了两个函数. void sse_mul_float(float *A, float *B, ...

  7. JQurey---新尝试

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  8. java 8新特性 匿名内部类的使用

    package com.atguigu.java8; import java.util.ArrayList; import java.util.Arrays; import java.util.Com ...

  9. Tclientdate的排序

    CDS_common.IndexDefs.Clear;        CDS_common.AddIndex('JSPH','JSPH',[],'JSPH');        CDS_common.A ...

  10. 【Codeforces 356A】Knight Tournament

    [链接] 我是链接,点我呀:) [题意] n个人矩形m场比赛 每场比赛由编号为li~ri且之前没有被淘汰的人进行. 已知第i场的winner是xi winner会把这一场其他所有的人都淘汰. 问你n个 ...