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.

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!

思路分析:从左至右,针对当前的高度,往右寻找,如果有大于等于当前高度的元素,则说明水平线应该拉到这儿,否则,应该找到在后面元素中相对最大的元素(如果有很多,则应该返回第一个元素)。cur指向这个返回的元素。

class Solution {
public:
int next(vector<int>& height, int pos){//若果pos位置后面有大于等于height[pos]直接返回第一个这样的值的位置,否则,返回后续中(最大的高度并且是第一次出现)的下标位置
if (pos >= height.size() - )//遍历到最后一个元素时,就应该结束了,因为蓄不了水
return -;
int val = height[pos];
int start = height[pos + ];
int res = pos + ;
for (int i = pos + ; i<height.size(); i++)
{
if (height[i] >= val){//情况之一,后面元素存在大于等于当前元素高度的值
return i;
}
else if (height[i]>start){//情况之二,记录后面元素中相对最大的元素,并且是第一次出现的元素
res = i;
start = height[i];
}
}
return res;
}
int trap(vector<int>& height) {
if(height.size()==)
return ;
int result = ;
int start = ;
while (height[start] == ){
start++;
}
while (start<height.size()-){
int cur = next(height, start);
int altitude = height[start]<height[cur] ? height[start] : height[cur];
for (int i = start + ; i<cur; i++){
result += (altitude - height[i]);
}
start = cur;
}
return result;
}
};

42. Trapping Rain Water的更多相关文章

  1. LeetCode - 42. Trapping Rain Water

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

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

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

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

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

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

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

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

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

  6. 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 用双指针向中间滑动,较小的高度就作为当前情 ...

  7. 刷题42. Trapping Rain Water

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

  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 ...

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

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

随机推荐

  1. 如何在HTML中加载Flash(2种实现方法)_HTML/Xhtml_网页制作

    点评:如何在HTML中加载Flash,为网页添加更多的色彩,普通的网页以无法满足用户的需求,接下来为大家介绍下2种在HTML中加载Flash的方法,感兴趣的各位可以适当参考下,希望对你有所帮助 第一种 ...

  2. JavaScript中的事件

    1.冒泡事件:事件按照特定的的事件目标到最不特定的事件目标顺序触发(它是按照DOM的层次节后依次做出的反应) 2.捕获事件:事件从不确定的对象document 开始触发然后到最精确(也可以在窗口级别捕 ...

  3. 64位win7下powerdesigner15连接postgresql9.2问题解决

    win7下已经安装jdk1.6 64bit版 安装powerdesigner 15,下载了postgressql jdbc驱动(下载地址:http://jdbc.postgresql.org/down ...

  4. 关于16年2月14日以后上传AppStore出现:Missing iOS Distribution signing identity for...的问题

    2016年2月14日以后打包上传AppStore会发现出现如下的问题: 导致问题的原因是:下边这个证书过期了 以下是苹果官方给出的回应: Thanks for bringing this to the ...

  5. GTD

    这两天坚持GTD,四分象限法管理时间,感觉学习专注度明显提升,一直没完成的马士兵JAVA基础整到第八章了,继续保持,1.13号前争取11章全整完. 3个点支撑起你的职业发展:技术,管理(管理自己.管理 ...

  6. inline(内联)函数

    1,为小操作定义一个函数的好处是:     a.可读性会强很多.     b.改变一个局部化的实现比更改一个应用中的300个出现要容易得多     c.函数可以被重用,不必为其他的应用重写代码     ...

  7. (转)modelsim10.0C编译ISE14.7的xilinx库(xilinx ip核)

    原地址modelsim10.0C编译ISE14.7的xilinx库(xilinx ip核)   1.打开D:\Xilinx\14.7\ISE_DS\ISE\bin\nt64\compxlibgui.e ...

  8. IIS内存溢出-设置IIS的应用程序池

    在ASP.NET Web服务器上,ASP.NET所能够用到的内存,通常不会等同于所有的内存数量.在machine.config(C:/WINDOWS/Microsoft.NET/Framework/v ...

  9. Visual Studio 2015 下 编译 libpng

    libpng https://github.com/glennrp/libpng zlib https://github.com/madler/zlib/releases https://github ...

  10. C++ STL 助记1:vector

    vector<, ); // Creates vector of 10 ints with value 100 vector<, "hello"); vector< ...