【LeetCode】42. Trapping Rain Water
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!
双指针left,right分别从首尾开始扫,记当前left指针遇到的最大值为leftWall,right指针遇到的最大值为rightWall
(1)leftWall <= rightWall
left前进一个位置。
对于left指针指向的位置,若存在被trap,则被trap的值为(leftWall-A[left])。
解释如下:
a.如果left与right之间不存在比leftWall大的值,那么i位置trap的值就取决与leftWall与rightWall的较小值,也就是leftWall
b.如果left与right之间存在比leftWall大的值,其中离leftWall最近的记为newLeftWall,那么i位置trap的值就取决与leftWall与newLeftWall的较小值,也就是leftWall
(2)leftWall > rightWall
right后退一个位置。
对于right指针指向的位置,被trap的值为(rightWall-A[right])。
解释同上。
class Solution {
public:
int trap(int A[], int n) {
int ret = ;
int left = ;
int right = n-;
int leftWall = A[left];
int rightWall = A[right];
while(left < right)
{
if(leftWall <= rightWall)
{
left ++;
if(A[left] <= leftWall)
ret += (leftWall - A[left]);
else
leftWall = A[left];
}
else
{
right --;
if(A[right] <= rightWall)
ret += (rightWall - A[right]);
else
rightWall = A[right];
}
}
return ret;
}
};
【LeetCode】42. Trapping Rain Water的更多相关文章
- 【LeetCode】42. Trapping Rain Water 接雨水 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 保存左右最大值 单调栈 日期 题目地址:ht ...
- 【一天一道LeetCode】#42. Trapping Rain Water
一天一道LeetCode系列 (一)题目 Given n non-negative integers representing an elevation map where the width of ...
- 【LeetCode】042 Trapping Rain Water
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
- 【LeetCode题意分析&解答】42. Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- LeetCode OJ 42. Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- leetcode problem 42 -- Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 【Lintcode】364.Trapping Rain Water II
题目: Given n x m non-negative integers representing an elevation map 2d where the area of each cell i ...
- 【Lintcode】363.Trapping Rain Water
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
随机推荐
- Wordpress中文章的特色图像Featured Image究竟存在哪里?
最近项目需要,分析了一下Wordpress的特色图像 Feature Image的上传.保存方式,这一分析觉得Wordpress的数据结构设计还真是有想法. 先简单说一下结论: Wordpress中图 ...
- 深入C++的new
new”是C++的一个关键字,同时也是操作符.关于new的话题非常多,因为它确实比较复杂,也非常神秘,下面我将把我了解到的与new有关的内容做一个总结. new的过程 当我们使用关键字new在堆上动态 ...
- Ubuntu下设置服务自启动
Ubuntu下设置服务自启动 一般/etc下可能还有/etc/rc.local,/etc/rc.sysinit文件,一般/etc/rc.local默认并不做什么实事,可能是系统留下的一个接口,供用户添 ...
- Android实现仿qq侧边栏效果
最近从github上看到一个关于侧边栏的项目,模仿的是qq侧边栏. Github地址是https://github.com/SpecialCyCi/AndroidResideMenu ,这个项目是一个 ...
- liunx step by step(3)
由于我的ubuntu是装载在virtualbox的虚拟机中,怎么实现其中的数据的共享,显得非常的必要:额,具体这么办. 1.安装增强功能.挂载光盘. 打开其相应的终端:fantlam@fantlam- ...
- supervisord不重启更新配置文件
二.更新新的配置到supervisord supervisorctl update 1 三.重新启动配置中的所有程序 supervisorctl reload
- Sqlserver存储过程生成日期维度
话不多说,之前已经有一篇日志是利用oracle的存储过程生成日期维度表,接下来我们就用sqlserver来实现这个操作,如下面的步骤所示 1:创建日期维度表(Dim_time) USE [DW] GO ...
- myeclipse查询mysql出来的汉字是乱码
乱码肯定是编码和解码方式不一致产生的! 1: 在mysql 中输入 status 可看到db 编码是什么 可选utf-8 or gbk 2: 连接时jdbc:mysql://serv ...
- [jQuery] Custom event trigger
$(document).ready(function(){ // Get Weather $('button').on('show.weather', function() { var results ...
- C++:fread、fwrite函数用法
主要内容: fread.fwrite函数的用法 1.函数功能 用来读写一个数据块. 2.一般调用形式 fread(buffer,size,count,fp); fwrite(buffer,size,c ...