题目要求: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. Linux 系统编程 学习:04-进程间通信2:System V IPC(1)

    Linux 系统编程 学习:04-进程间通信2:System V IPC(1) 背景 上一讲 进程间通信:Unix IPC-信号中,我们介绍了Unix IPC中有关信号的概念,以及如何使用. IPC的 ...

  2. 公钥、私钥、SSL/TLS、会话密钥、DES【转载】

    原文链接:https://www.cnblogs.com/thbCode/p/5829719.html 一,公钥私钥1,公钥和私钥成对出现2,公开的密钥叫公钥,只有自己知道的叫私钥3,用公钥加密的数据 ...

  3. C++代码雨

    闲逛的时候发现了一个很好玩的程序 摘自:https://blog.csdn.net/u012837895/article/details/20849967#comments 效果如下 #include ...

  4. ZOJ 1091 Knight Moves(BFS)

    Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where you are t ...

  5. VirtualBox 6 安装 CentOS 7

    1 安装环境 windows7 Oracle VM VirtualBox 6.0.24 CentOS 7 2 VirtualBox 6 - 虚拟机软件 2.1 下载 Oracle VM Virtual ...

  6. day001|python基础回顾

    1.python是一门编程语言 编程语言是人与计算机能够沟通的一种语言 人----------编程语言---------计算机 2.编程是: ①人将要做的事的步骤想清楚 ②找计算机能够听懂的语言将步骤 ...

  7. binary hacks读数笔记(od命令)

    Linux od命令用于输出文件内容. od指令会读取所给予的文件的内容,并将其内容以八进制字码呈现出来 -t<输出格式>或--format=<输出格式> 设置输出格式. 实例 ...

  8. spring中的事务有两种方式

    1种是我们常用的声明式事务,如注解,或者配置文件配置的. 2种是编程式事务,如 TransactionTemplate 类的使用.

  9. 老板让只懂Java基本语法的我,基于AQS实现一个锁

    10 点整,我到了公司,又成为全组最后一个到的员工. 正准备刷刷手机摸摸鱼,看见老板神秘兮兮地走了过来. 老板:闪客呀,你写个工具,基于 AQS 实现一个锁,给咱们组其他开发用 我:哦好的 老板:你多 ...

  10. MySQL存储索引InnoDB数据结构为什么使用B+树,而不是其他树呢?

    InnoDB的一棵B+树可以存放多少行数据? 答案:约2千万 为什么是这么多? 因为这是可以算出来的,要搞清楚这个问题,先从InnoDB索引数据结构.数据组织方式说起. 计算机在存储数据的时候,有最小 ...