题目要求: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. Serilog 源码解析——总览

    背景 大家好,考虑到在最近这些天,闲来无事,找了个类库好好研究下别人写的高质量代码,颇有收获,打算和大家分享下.考虑到最近在自学 ASP.NET Core 的相关开发,对 Serilog 这个日志记录 ...

  2. 机器学习 第4篇:sklearn 最邻近算法概述

    sklearn.neighbors 提供了针对无监督和受监督的基于邻居的学习方法的功能.监督的基于最邻近的机器学习算法是值:对带标签的数据的分类和对连续数据的预测(回归). 无监督的最近算法是许多其他 ...

  3. 用GitHub Pages搭建博客(三)

    本篇介绍通过git工具替换网站主题,并发布 Jekyll和Hexo的简要介绍   GitHub Pages是基于Jekyll构建的,Jekyll 是一个简单的博客形态的静态站点生产工具,它有一个模版目 ...

  4. Windows Server 2012 R2 辅助域控制器搭建

    Windows Server 2012 R2 辅助域控制器搭建 以下操作都是基于主域已搭建成功的基础上,全程操作过程都是在辅域上操作完成. 地址 主域:10.228.81.207 辅域:10.228. ...

  5. python开发基础(二)运算符以及数据类型之float(浮点类型)

    # encoding: utf-8 # module builtins # from (built-in) # by generator 1.147 """ Built- ...

  6. React react-redux props或state更新视图无法重新渲染问题

    记录学习React时自己是如何挖坑把自己埋了的过程:children以及其它props被修改时相关组件无法重新渲染(做了两天) 父组件代码: class UserHome extends Compon ...

  7. 数据结构 - 二叉树的遍历(递归VS非递归)

    import java.util.LinkedList; public class BinaryTree { public static void main(String[] args) { int ...

  8. 美团笔试题_ACM最多队伍

    题目: 选择队伍参加ACM比赛,可以选择的人由N+M组成,N代表N个擅长算法的人,M代表M个擅长编程的人,选择要求:每个队伍三个人,每个队伍中至少有一个擅长算法的人,至少有一个擅长编程的人.求可以组成 ...

  9. 【转载】VirtualBox 扩展增强包安装

    1 扩展包作用 鼠标可自动在虚拟机和物理机中切换状态,而不用按快捷键解除独占功能 安装了扩展包后,可以解决 virtualbox 中 更改 ubuntu 分辨率无效的问题 2 原文地址 星朝 - Vi ...

  10. C#两行代码实现三维地球

    一.             为什么要用三维地球? 三维地球是地理信息技术的一个重要发展方向,相比较二维地图技术,三维地球最大的特点是更直观更形象地表达地理信息和空间上的方位.我们可以在三维气象模拟. ...