42. Trapping Rain Water
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的更多相关文章
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
- [array] leetcode - 42. Trapping Rain Water - Hard
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- 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 用双指针向中间滑动,较小的高度就作为当前情 ...
- 刷题42. Trapping Rain Water
一.题目说明 题目是42. Trapping Rain Water,翻译起来就是"接雨水".给n个非负正数代表高度,每个正数宽度为1,让计算能多少雨水.题目难度是Hard 二.我的 ...
- [LeetCode] 42. Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 【LeetCode】42. Trapping Rain Water
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
- leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
随机推荐
- window go protobuf
http://studygolang.com/articles/8804 protoc --go_out=. protocol.proto E:\TEST\TESTGRPC\src\google.go ...
- sed 命令使用
ios 的sed 命令 跟linux sed 命令有区别 # 所有的a 替换成b sed -i "" 's/a/b/g' #删除掉所有包含a的行 sed -i "/a/ ...
- scala 学习之:list span 用法
Pack consecutive duplicates of list elements into sublists. If a list contains repeated elements the ...
- EL使用:打印集合
<%@page import="java.util.HashMap"%><%@page import="java.util.Map"%> ...
- TimeQuest学习
1.物理时钟特性:clock skew(时钟差),jitter(拉动),clock latency(时钟潜伏),这些物理时钟特性又称为uncertainl--非定性,或非理想性. clock skew ...
- 做webapp静态页面的一些积累
1)根据pad,手机不同的版本的屏幕大小设置字体的大小(可以使用此方式根据屏幕的不同进行设置,由于我左边的图片是设置的float='left',使用的是img标签的百分比来显示图片) (使用此方式, ...
- php_access_ADOConn 备忘
1.咕~~(╯﹏╰)b 各种乱码. 2.本来是MFC+Access 用msado15.dll写的系统,闲的想用php改改. Demo.php <meta http-equiv="Con ...
- shell 中scp密码输入 --expect
这里必须先安装: yum install expect -y expect是一种自动交互语言,能实现在shell脚本中为scp和ssh等自动输入密码自动登录. 下面给出scp和ssh的使用示例: 1. ...
- Android环境配置及运行helloWord案例
Android的环境搭建步骤,以及输出一个helloWorder 1:下载Android开发环境 及是: SDK adt-bundle-windows-x86_64-20140702 此时的版 ...
- 计算软键盘的高度然后确定自定义的View的具体位置
singleTouchView.getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayout ...