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!

分析: 对于每一点只需要知道其左边和右边的最高山即可

class Solution {
public:
int trap(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(n <= ) return ; vector<int> left(n,);
vector<int> right(n, ); left[] = A[];
for(int i =; i < n;++i)
{
left[i] = left[i-] > A[i] ? left[i-] : A[i];
} right[n-] = A[n-];
for(int i = n-; i>= ;--i){
right[i] = right[i+] > A[i] ? right[i+] : A[i];
} int res = ;
for(int i = ; i< n; i++){ int min = left[i] < right[i]? left[i] : right[i];
res += min - A[i];
} return res;
}
};

LeetCode_Trapping Rain Water的更多相关文章

  1. [LeetCode] Trapping Rain Water II 收集雨水之二

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  2. [LeetCode] Trapping Rain Water 收集雨水

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

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

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

  4. Leetcode Trapping Rain Water

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

  5. 42. Trapping Rain Water

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

  6. Trapping Rain Water

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

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

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

  8. LeetCode - 42. Trapping Rain Water

    42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...

  9. Leetcode: Trapping Rain Water II

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

随机推荐

  1. hibernate之关系映射上

    分别创建user,farm,user_general三张表 create table user( uuid bigint not null auto_increment, name ), age in ...

  2. Oracle_Q&A_02

    Step02: Import SQL FILE Step02: QUESTION & ANSWER --1查询员工姓名和职位字数相等的员工 --2查询e_name以s结尾的员工(不用like) ...

  3. 解决Xcode8 输出一对字符串问题

    在Product->Scheme->Edit Scheme->Run->Environment Variables下添加键:OS_ACTIVITY_MODE, 值:Disabl ...

  4. mongoose--------ORM数据操作框架

    数据关系映射:ORM O:object R:relation M:mapping 把对数据库的操作都封装到对象中,操作了对象,就相当于操作了数据库. 安装: npm install mongoose ...

  5. es6新特性:

    http://es6katas.org/ es6+一些新特性,截图如下 对应方法,函数显示相关的数据,如图: 对应方法,函数的例子,如下

  6. event.getAction()&MotionEvent.ACTION_MASK的原因

    看到下面代码中用了AND位运算是为了什么呢? public boolean onTouchEvent(MotionEvent event) { int action = event.getAction ...

  7. linux diff具体解释

    diff是Unix系统的一个非常重要的工具程序. 它用来比較两个文本文件的差异,是代码版本号管理的基石之中的一个.你在命令行下,输入: $ diff <变动前的文件> <变动后的文件 ...

  8. zend_db连接mysql(附完整代码)(转)

    在看这些之前请确保你正确加载了PDO扩展. 作法是编辑php.ini手动增加下面这两行(前面要没有分号;):extension=php_pdo.dllextension=php_pdo_mysql.d ...

  9. JavaScript中函数参数的按值传递与按引用传递(即按地址传递)

    首先声明一句:JavaScript中所有函数的参数都是按值传递的!不存在按引用传递! 在讲传递参数之前我们先来讲一下指针. 学过C指针的应该都知道,指针变量中保存的是一个地址,程序可以根据所保存的地址 ...

  10. Linux 数据 CD 刻录

    http://www.cyberciti.biz/tips/linux-burning-multi-session-cds-on-linux.html #mkisofs -dvd-video -inp ...