题目要求: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. PHP 将数组转换为JSON字符串<兼容中文>

    1 /************************************************************** 2 * 3 * 使用特定function对数组中所有元素做处理 4 ...

  2. 【Luogu】P1072 Hankson 的趣味题 题解

    原题链接 嗯...通过标签我们易得知,这是一道数学题(废话) 其中,题目给了这两个条件: \(gcd(x,a_0)=a_1,lcm(x,b_0)=b_1\) 所以,根据 \(gcd\) 与 \(lcm ...

  3. c#视频位置

    static void Main(string[] args)        { string scoure = @"C:\Documents and Settings\Administra ...

  4. python的数据处理一

    def load_data(filename): features = [] labels = [] f = open(filename, encoding='utf-8') medical = js ...

  5. python爬虫07BeautifulSoup

    有一个高效的网页解析库 它的名字叫做 BeautifulSoup 它   是一个可以从 HTML 或 XML 文件中提取数据的 Python 库 首先我们要安装一下这个库 pip install be ...

  6. 常用简单电脑bai快捷键大全

    Ctrl+C 复制.duCtrl+X 剪切.Ctrl+V粘贴.Ctrl+Z撤销.Ctrl+A全选所有文件.zhiDelete删除.daoShift+Delete避开回收站直接永久删除(不可找回).F3 ...

  7. 记载idea创建spring-boot项目时“Spring Initalizr Error”的问题处理

  8. 把数据转化为JSON格式用ajax进行前后端交互

    接着在https://www.cnblogs.com/dong973711/p/10907733.html的基础上做验证. 从前端提交数据 前端页面,submit.html <!DOCTYPE ...

  9. 后端狗的Vue学习历程(一) - demo示例与基本逻辑语法

    目录 demo的三部分结构 判断:v-if.v-else-if.v-else 循环:v-for 事件绑定 v-on:eventType 内容输入的双向绑定v-model 源码:Github demo的 ...

  10. 2. Hive常见操作命令整理

    该笔记主要整理了<Hive编程指南>中一些常见的操作命令,大致如下(持续补充中): 1. 查看/设置/修改变量2. 执行命令3. 搜索相关内容4. 查看库表信息5. 创建表6. 分区7. ...