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 ...
随机推荐
- plsqldeveloper永久注册码
注册码:Product Code:4t46t6vydkvsxekkvf3fjnpzy5wbuhphqzserial Number:601769 password:xs374ca
- 关于使用IQKeyBoardManager键盘还是被遮挡的问题解决方案
今天在做一个登录界面的时候发现使用了IQKeyBoardManager键盘还是被遮挡,解决方案如下 解决方案一:在所有视图的最外层添加一个UIView作为容器即可,但在有导航栏的情况下导航栏会跟着向上 ...
- 开发者神器!Windows上最强大的虚拟桌面工具-Dexpot
简介 : 用过Linux和Mac计算机的人都知道 , 这两个系统上都有个多桌面的功能非常使用 . 而在Windows上并没有该项功能 , 虽然目前Win10已经增加了多桌面的程序 , 但使用上仍体验不 ...
- CSS选择器优先级【转】
样式的优先级 多重样式(Multiple Styles):如果外部样式.内部样式和内联样式同时应用于同一个元素,就是使多重样式的情况. 一般情况下,优先级如下: (外部样式)External styl ...
- CodeForces - 816C Karen and Game(简单模拟)
Problem Description On the way to school, Karen became fixated on the puzzle game on her phone! The ...
- ACM_LRU页面置换算法
LRU页面置换算法 Time Limit: 2000/1000ms (Java/Others) Problem Description: sss操作系统没听课, 这周的操作系统作业完全不会, 你能帮他 ...
- MVC模式到传统风格的Spring MVC
现在我们要做个简单的基于servlet的MVC的模型,我们要有一个Product要从表单处获取. MVC中的M是模型,V是视图,C是控制器.视图负责应用的展示,模型封装了数据和业务逻辑,控制器负责接收 ...
- Kotlin学习的一些心得
1.工程manifest中的application节点有冲突时,添加 xmlns:tools="http://schemas.android.com/tools" tools:re ...
- UVM挑战及概述
UVM的调度也具有其独特的挑战,尤其是在调试的领域.其中的一些挑战如下: 1. Phase的管理:objections and synchronization 2. 线程调试 3. Tracing i ...
- 工作中Docker使用命令笔记
docker安装与启动 安装docker [root@localhost /]# yum -y install docker-io 更改配置文件 [root@localhost /]# vi /etc ...