题目要求: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!

代码如下:

class Solution {
public:
int trap(int A[], int n) { int maxIdx = 0;
int water = 0; //找到最长的木板,设为maxIdx
for(int i = 1; i < n; i++){
if(A[i] > A[maxIdx]){
maxIdx = i;
}
} int max = A[0]; //左侧逼近
for(int i = 1; i < maxIdx; i++){ if(max < A[i])
max = A[i];
//木板左边(max)和右边(最高)都比它高,则可以放
// max - 该木板长度 的水
else
water += max - A[i];
} //右侧逼近
max = A[n - 1];
for(int i = n - 2; i >= maxIdx; i--){
if(max < A[i]) max = A[i];
else water += max - A[i];
} return water; }
};

LeetCode 042 Trapping Rain Water的更多相关文章

  1. [leetcode][042] Trapping Rain Water (Java)

    我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步. 题目在这里: ...

  2. Java for LeetCode 042 Trapping Rain Water

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

  3. leetcode#42 Trapping rain water的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...

  4. [array] leetcode - 42. Trapping Rain Water - Hard

    leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...

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

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

  6. [LeetCode] 407. Trapping Rain Water II 收集雨水 II

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

  7. [LeetCode] 42. Trapping Rain Water 收集雨水

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

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

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

  9. LeetCode - 42. Trapping Rain Water

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

随机推荐

  1. Spring中的BeanFactory与FactoryBean看这一篇就够了

    前言 理解FactoryBean是非常非常有必要的,因为在Spring中FactoryBean最为典型的一个应用就是用来创建AOP的代理对象,不仅如此,而且对理解Mybatis核心源码也非常有帮助!如 ...

  2. [Luogu P4173]残缺的字符串 ( 数论 FFT)

    题面 传送门:洛咕 Solution 这题我写得脑壳疼,我好菜啊 好吧,我们来说正题. 这题.....emmmmmmm 显然KMP类的字符串神仙算法在这里没法用了. 那咋搞啊(或者说这题和数学有半毛钱 ...

  3. 微信小程序开发之云开发

    创建云开发小程序项目 开通云开发 开通后界面 选择开发环境 开启使用npm模块 安装wx-server-sdk npm install --save wx-server-sdk@latest 创建云函 ...

  4. Thinkphp3.2 cms之权限管理

    五.权限管理 <?php namespace Admin\Controller; use Think\Controller; class CommonController extends Con ...

  5. c#导入文件

    string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Administrator\Desktop\2.txt",Encod ...

  6. 为什么大多数IOC容器使用ApplicationContext,而不用BeanFactory

    1. 引言 Spring框架附带了两个IOC容器– BeanFactory 和 ApplicationContext. BeanFactory是IOC容器的最基本版本,ApplicationConte ...

  7. kali中PHANTOM-EVASION 3.0运行时module ' OpenSSL.crypto' has no attribute ' PKCS12Type'报错解决方法

    kali中直接用pip install pyopenssl安装当最新的openssl中的crypto中的类PKCS12Type改成了PKCS12 所以需要在phantom中改对应代码 将这个文件中对应 ...

  8. c++ 11字符串与string转换常用函数

    这里主要介绍一下string to int 其他方法与这个类似,可到头文件 <string> 中查看 @_Str 转换的字符串 @_Idx 转换的长度(位数) @_Base 进制 doub ...

  9. 蒲公英 · JELLY技术周刊 Vol.30: 此路不通?Vue 3 新提案 Ref-sugar

    蒲公英 · JELLY技术周刊 Vol.30 随着 Vue 3 发布,相关的新闻也逐渐火热起来,而近期 RFC 中两个新的提案也因为某乎上的一些事情变得广为人关注.Ref sugar和script s ...

  10. spring boot实现超轻量级网关(反向代理、转发)

    在我们的rest服务中,需要暴露一个中间件的接口给用户,但是需要经过rest服务的认证,这是典型的网关使用场景.可以引入网关组件来搞定,但是引入zuul等中间件会增加系统复杂性,这里实现一个超轻量级的 ...