LeetCode(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!
分析
AC代码
class Solution {
public:
int trap(vector<int>& height) {
if (height.empty())
return 0;
int len = height.size();
int lhs = 0, rhs = len - 1, secHeight = 0;
int area = 0;
//从两边向中间统计,分别统计每个竖立格子可以盛的水量
while (lhs < rhs)
{
if (height[lhs] < height[rhs])
{
secHeight = max(height[lhs], secHeight);
//加上lhs竖格的盛水量
area += secHeight - height[lhs];
//左侧右移一格
lhs++;
}
else{
secHeight = max(height[rhs], secHeight);
//加上rhs竖格的盛水量
area += secHeight - height[rhs];
//右侧左移一格
rhs--;
}//fi
}//while
return area;
}
};
LeetCode(42)Trapping Rain Water的更多相关文章
- (算法)Trapping Rain Water II
题目: Given n * m non-negative integers representing an elevation map 2d where the area of each cell i ...
- (算法)Trapping Rain Water I
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
- leetcode 第41题 Trapping Rain Water
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
- LeetCode 笔记系列12 Trapping Rain Water [复杂的代码是错误的代码]
题目:Given n non-negative integers representing an elevation map where the width of each bar is 1, com ...
- LeetCode(42):接雨水
Hard! 题目描述: 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度 ...
- LeetCode: Trapping Rain Water 解题报告
https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- [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 ...
随机推荐
- nginx缓存过期
1 原理 在默认下,请求过的内容会接受304,而从本地缓存调用.这是通过client向server发送请求,给出ETag,server确认ETag未变,则不返回内容,client调用本地缓存. 而ex ...
- AtCoder Regular Contest 081 F - Flip and Rectangles
题目传送门:https://arc081.contest.atcoder.jp/tasks/arc081_d 题目大意: 给定一个\(n×m\)的棋盘,棋盘上有一些黑点和白点,每次你可以选择一行或一列 ...
- ForeignKeyConstraint 外键约束的使用及作用的学习[转]
原文链接 da.SelectCommand.CommandText="select au_id,au_fname,au_lname from authors"; da.Fill(d ...
- Aappcloud 调到二级页面黑屏
PartnerHead3.html 后面多了一个点
- AJPFX总结内部类
内部类:内部类的访问规则:1. 内部类可以直接访问外部类中的成员,包括私有 原因是内部类中持有了一个外部类的引用,格式:外部类.this2. 外部类要访问内部类,必须建立内部类对象访问格式:1. ...
- Android如何用阿里云的API进行身份证识别
准备工作:在libs下添加 alicloud-Android-apigateway-sdk-1.0.1.jar,commons-codec-1.10-1.jar 在build.gradle添加 co ...
- laravel学习笔记(二)
路由 HTTP方法:支持http1.1中所有类型传参方式,get,post,put,delete,options,patch Route::get($url,$callback); 路由参数: Rou ...
- 云梯互联:所有主机已全面支持免费SSL!附小白配置教程。
HTTPS和HTTP的区别:1.HTTPS是加密传输协议,HTTP是名文传输协议;2.HTTPS需要用到SSL证书,而HTTP不用;3.HTTPS比HTTP更加安全,对搜索引擎更友好,利于SEO4. ...
- Netbeans调试教程
官方教程:Netbeans调试 CC++ 项目教程.docx 1.步过: 就是把函数当成一条指令来调用 比如上面就是光执行fun(i),不会到函数里面去 2.步入 就是进入函数里面执行 3.步出 就是 ...
- js运行机制(线程)
浏览器线程 js运作在浏览器中,是单线程的,即js代码始终在一个线程上执行,这个线程称为js引擎线程. 浏览器是多线程的,除了js引擎线程,它还有: UI渲染线程 浏览器事件触发线程 http请求线 ...