【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 ...
随机推荐
- springboot 启动类CommandLineRunner(转载)
在Spring boot项目的实际开发中,我们有时需要项目服务启动时加载一些数据或预先完成某些动作.为了解决这样的问题,Spring boot 为我们提供了一个方法:通过实现接口 CommandLin ...
- [Android Pro] Swift 3.0多线程
本文只介绍Grand Central Dispath(GCD) 中央调度 个人认为一个GCD就够用了,可能是改版或是其他的在找之前写的多线程方法时发现不能用了,看文档之后发现改了,现在看上去更加简单易 ...
- scala编程第16章学习笔记(4)——List对象的方法
通过元素创建列表:List.apply List(1, 2, 3) 等价于List.apply(1, 2, 3): scala> List.apply(1, 2, 3) res0: List[I ...
- How To Open An URL In Android’s Web Browser
How To Open An URL In Android’s Web Browser 以下核心代码片断,将展现使用“android.content.Intent” 打开一个指定的 URL. butt ...
- 结合实例详解"pure Virtual function called"
作者:阿波 链接:http://blog.csdn.net/livelylittlefish/article/details/9750593 (4年前的一篇文章,翻出来共享一下.) 本实例即为经典的讲 ...
- .NET MVC自定义错误处理页面的方法
在ASP.NET MVC中,我们可以使用HandleErrorAttribute特性来具体指定如何处理Action抛出的异常.只要某个Action设置了HandleErrorAttribute特性,那 ...
- Greenplum query Oracle via DLINK
在系统日常生产过程中.怎样从Oracle数据库同步一些配置表到GP库个问题. 先来说说之前使用的方式: 0. 使用第3方工具从Oracle导出纯文本格式,存放在本地: 1. 使用GP外部表载入入库 这 ...
- C#.NET常见问题(FAQ)-如何强制退出进程
可以使用Process.Kill方法,但是有时候执行完了该进程还在,要等一会才会自动关掉 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youku.com/ac ...
- TextView跑步灯效果及在特殊情况下无效的解决方式
概述: 关于在TextView中使用跑马灯效果的样例在网上一搜一大把.他们可能会让你像以下这样来在xml中定义TextView控件的属性.而事实也确是如此. 只是我不知道他们有没有遇到和我一样的问题( ...
- es6-块级作用域let 和 var的区别
块级绑定 js的处理机制和我们大家想象的完全不同,并不完全是所谓函数调用以及上下执行那样简单,它是存有”域”的本质区别的. var具有全局污染特性,所以es6才会出现let .const. 下面通过一 ...