题目描述

给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。

上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 感谢 Marcos 贡献此图。

示例:

输入: [0,1,0,2,1,0,1,3,2,1,2,1]
输出: 6

解题思路

基本思想是从数组首位置找到第一个大于其紧邻后面的数,将它作为第一个雨槽的左挡板,然后向后遍历找到第一个不小于它的的数作为右挡板,这两个高度之间即可构成一个容器,遍历中间的高度依次计算盛水单位,然后继续以此右挡板作为下一个雨槽的左挡板。如果没有找到不小于左挡板的高度,则把此段雨槽反过来重新按照之前的算法计算盛水容量。

代码

 class Solution {
public:
int trap(vector<int>& height) {
int res = ;
if(height.size() < ) return res;
int left = ;
while(left < height.size() - && height[left] <= height[left + ])
left++;
if(left == height.size() - ) return res;
int right = left + ;
while(right < height.size()){
if(height[right] >= height[left]){
for(int i = left + ; i < right; i++)
res += height[left] - height[i];
left = right;
right = left + ;
}
else if(right == height.size() - ){
vector<int> hr;
for(int i = right; i >= left; i--)
hr.push_back(height[i]);
res += trap(hr);
break;
}
else right++;
}
return res;
}
};

LeetCode 42. 接雨水(Trapping Rain Water)的更多相关文章

  1. [Swift]LeetCode42. 接雨水 | Trapping Rain Water

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

  2. leetcode 第41题 Trapping Rain Water

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

  3. LeetCode 笔记系列12 Trapping Rain Water [复杂的代码是错误的代码]

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

  4. LeetCode: Trapping Rain Water 解题报告

    https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...

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

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

  6. leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

  7. [LeetCode] 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 收集雨水 II

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

  9. [Leetcode][Python]42: Trapping Rain Water

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...

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

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

随机推荐

  1. [leetcode] 题解记录 11-20

    博客园markdown太烂, 题解详情https://github.com/TangliziGit/leetcode/blob/master/solution/11-20.md Leetcode So ...

  2. Vue访问权限

    设置权限 <script> export default { created(){ if(this.$store.state.userStore.role !== 'manager'){ ...

  3. 一步步创建第一个Vue项目

    写在了GitHub https://github.com/TaoPanfeng/vue-cms 1 初始化 创建一个文件夹 vue-cms 在vue-cms目录下创建文件 package.json 在 ...

  4. three.js之元素周期表

    <html><head> <title>three.js css3d - periodic table</title> <meta charset ...

  5. C++归并排序(数组&链表)

    1.归并排序(Merge Sort) 归并排序的性能不受输入数据的影响,始终都是O(n log n)的时间复杂度.代价是需要额外的内存空间. 归并排序是建立在归并操作上的一种有效的排序算法.该算法是采 ...

  6. JMeter 控件整理

    一个线程组里多个请求的情况下,添加用户参数.如添加random函数,若不勾选“每次迭代更新一次”则每发送一个请求调用一次random函数,勾选上之后,整个线程组运行过程只调用一次random函数.

  7. 转载一篇c++开源框架和库

    值得学习的C语言开源项目 -1. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的性 ...

  8. BOOST 解析,修改,生成xml样例

    解析XML 解析iworld XML,拿到entity和VisibleVolume的数据 int ParseiWorlds::readXML(const bpath &dir) { ptree ...

  9. 白话Spring(基础篇)---AOP(execution表达式)(转)

    [一知半解,就是给自己挖坑] 作为AOP的最后一节内容,我们来简单总结一下切面表达式上见的书写方法.下面的那内容有参考其他博文,在此先对开源博客的各位大神表示感谢! ----------------- ...

  10. qt一些函数

    sleep(n);   休息ns msleep(1000);   休息1000毫秒 usleep(1000000);  //休息n微妙