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 ...
随机推荐
- mac 配置nginx 虚拟域名(转载)
我是通过homebrew 安装nginx 的,所以安装目录是默认的,之前多个server都是放在默认安装目录下的nginx.conf里的,但是这样不太好,就是会导致nginx.conf 越来越长,而且 ...
- Python实现两已知排好序的列表合并成一个排好序的列表
#方法0.5--- lst1 = [1, 3, 7, 9, 12] lst2 = [4, 8, 9, 13, 15, 19] def merge(a, b): c = [] h = j = 0 whi ...
- Travelling HDU - 3001
Travelling HDU - 3001 方法:3进制状态压缩dp(更好的方法是预处理出每个状态数字对应的y数组,然后用刷表,时间复杂度可以少一个n) #include<cstdio> ...
- 18.5.1使用Proxy和InvocationHandler创建动态代理
package d18_5_1; public interface Person { void walk(); void sayHello(String name); } package d18_5_ ...
- 用eclipse-inst-win64.exe安装eclipse出现Java for Windows Missing 的原因
Java for Windows Missing 因为jdk的版本没有对,我这里是64位的机器上安了32位的jdk,所以一直报这个. 必须换上相对应版本的jdk,提示页面有链接,直接点击就可以下载. ...
- iOS7改变状态栏文字颜色
1在Info.plist中设置UIViewControllerBasedStatusBarAppearance 为NO2 在需要改变状态栏颜色的 AppDelegate中在 didFinishLaun ...
- qconshanghai2016
http://2016.qconshanghai.com/schedule 大会日程 2016年10月20日 星期四 07:45 开始签到 09:00 开场致辞 专题 前端技术实践 主题演讲 业务上云 ...
- Asp.net 字符(一)
1.字母大小写处理 private string GetChangedStr(string oldStr, strType type) { string newStr = ""; ...
- asp.net MVC 错误信息“没有为该对象定义无参数的构造函数”请求各位大神帮忙!
在做一个登录的功能,没有用MVC自己生成的identity代码,仿照别人的代码写出了以后出现错误. 错误信息如下: 代码如下: 求各位asp.net大神支招,网上找了资料最终也没解决这个问题.
- 微服务熔断限流Hystrix之流聚合
简介 上一篇介绍了 Hystrix Dashboard 监控单体应用的例子,在生产环境中,监控的应用往往是一个集群,我们需要将每个实例的监控信息聚合起来分析,这就用到了 Turbine 工具.Turb ...