题目:

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.

 
Example

Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.

题解:

  读题可知,区间[a,b]内容积由边缘最小值决定,Two pointer思想,遍历整个数组。每一次都是寻找两端最小值,然后开始遍历,遇到更大值后替换端值(不能盛水了)。

Solution 1 () (from here 九章算法)

class Solution {
public:
int trapRainWater(vector<int> &heights) {
int left = , right = heights.size() - ;
int res = ;
if (left >= right) {
return res;
}
int lheight = heights[left];
int rheight = heights[right];
while (left < right) {
if (lheight < rheight) {
++left;
if (lheight > heights[left]) {
res += lheight - heights[left];
} else {
lheight = heights[left];
}
} else {
--right;
if (rheight > heights[right]) {
res += rheight - heights[right];
} else {
rheight = heights[right];
}
}
}
return res;
}
};

  其他解法见此处

【Lintcode】363.Trapping Rain Water的更多相关文章

  1. 【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 ...

  2. 【LeetCode】42. Trapping Rain Water

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

  3. 【LeetCode】042 Trapping Rain Water

    题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...

  4. 【LeetCode】42. Trapping Rain Water 接雨水 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 保存左右最大值 单调栈 日期 题目地址:ht ...

  5. 【一天一道LeetCode】#42. Trapping Rain Water

    一天一道LeetCode系列 (一)题目 Given n non-negative integers representing an elevation map where the width of ...

  6. 【LeetCode题意分析&解答】42. Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  7. LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))

    LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...

  8. [LintCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  9. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

随机推荐

  1. c++的检测的确比C++更严格

    见下面代码 #include <stdio.h> #include <stdlib.h> #include <time.h> enum guess { paper, ...

  2. WINDOWS的用户和用户组说明

    1.基本用户组 Administrators 属于该administators本地组内的用户,都具备系统管理员的权限,它们拥有对这台计算机最大的控制权限,可以执行整台计算机的管理任务.内置的系统管理员 ...

  3. nginx访问日志中的时间格式修改

    1.说明 默认的时间格式是:[08/Mar/2013:09:30:58 +0800],由$time_local变量表示. 我想要改成如下格式:2013-03-08 12:21:03. 2.需要修改的文 ...

  4. Android5.0以后版本把应用移动到SD或者TF卡的方法

    由于手机内存较小,才8G,用的时间一久,内部存储就满了,天天删垃圾,WIFI还老断线,终于忍无可忍了,决定把应用移动到SD卡,实践下来,只有少部分App默认支持移动到SD卡,大部分程序不支持只能装在内 ...

  5. Delphi 对话框实现源码分析

    Delphi 对话框实现源码分析   简介 在这篇文章中,我将大概的从Delphi XE2 的Dialogs单元入手,分析ShowMessage,MessageBox等对话框运行原理,希望能帮助你理解 ...

  6. js:深入函数的定义

    函数定义方式: 1.function fun1(){alert("fun1");}  //函数就是一个很特殊的对象.是一个Function的实例.事实上在内存中存储的操作是通过一个 ...

  7. ABAP内表数据做层次XML输出

      *&---------------------------------------------------------------------**& Report  Z_BARRY ...

  8. 教你在 Yii2 中添加全局函数

    方法一 这种方法就是直接在入口文件web/index.php里面写函数,示例代码如下: // something code …… // 全局函数 function pr($var) { $templa ...

  9. Python 3 mysql 简介安装

    Python 3 mysql 简介安装 一.数据库是什么 1.  什么是数据库(DataBase,简称DB) 数据库(database,DB)是指长期存储在计算机内的,有组织,可共享的数据的集合.数据 ...

  10. web框架之Django<一、初识>

    一.什么是web框架 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演. 对于所有的 ...