题目:

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. maven-tomcat7;IOC;AOP;数据库远程连接

    [说明]真的是好烦下载插件啊,maven-tomcat7 插件试了好多次都不行,下载不成:部署不成:好不容易从github中得到的springmvc项目也是运行不起来,中间又是查了许多东西,绕着绕着都 ...

  2. [Sdoi2013]随机数生成器(BSGS)

    #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #inclu ...

  3. Idea 使用的技巧和设置

    1.自动提示时候,忽绿大小写, setting---->sensitive 2:IntelliJ IDEA报错class is never used 图中的unused declaration选 ...

  4. transport connector和network connector

    1 什么是transport connector 用于配置activemq服务器端和客户端之间的通信方式. 2 什么是network connector 用于配置activemq服务器之间的通信方式, ...

  5. activiti--6-------------------------------------连线(一般数据库表的查询顺序)

    一.流程图 二.这次把流程图和Java类放在一个包下 三.代码 package com.xingshang.f_sequenceFlow; import java.io.InputStream; im ...

  6. linux 7- - watch,free,mpstat,vmstat,iostat,pidstat,df,du

    十八.  和系统运行状况相关的Shell命令:     1.  Linux的实时监测命令(watch):     watch 是一个非常实用的命令,可以帮你实时监测一个命令的运行结果,省得一遍又一遍的 ...

  7. python发布包流程

    1.新建文件夹suba和subb,文件夹下新建__init__.py,内容可以为空 2.suba内新建文件aa.py bb.py 3.subb内新建文件cc.py dd.py 4.setup.py文件 ...

  8. Linux vim 中文显示乱码解决方法

    因为在windows下默认是gb编码,而我的vim默认是utf-8(gedit默认也是utf-8),所以打开会成乱码.改动了一下配置文件,使vi支持gb编码就好了.$vi ~/.vimrclet &a ...

  9. 用linux搭建ranzhi环境

    一.安装红帽6.5 1.安装时需选择桥接模式: 2.选择自定义,在设置中将镜像文件(ISO)选择进去: 3.安装时选择[桌面]安装(在/etc/inittab文件中,若id=5则为桌面模式,id=3为 ...

  10. 【leetcode刷题笔记】Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...