题目描述

给定 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. Centos7:MyCat安装,配置及使用

    解压缩 使用,默认端口8066 ./mycat start //启动 ./mycat stop //停止 ./mycat restart //重启

  2. go之环境安装

    1)Linux安装 https://studygolang.com/dl # 在 ~ 下创建 go 文件夹,并进入 go 文件夹 mkdir ~/go && cd ~/go 下载的 g ...

  3. OSI七层协议模型、TCP/IP四层模型

    OSI七层协议模型 TCP/IP四层模型 首先我们梳理一下每层模型的职责: 链路层:对0和1进行分组,定义数据帧,确认主机的物理地址,传输数据: 网络层:定义IP地址,确认主机所在的网络位置,并通过I ...

  4. 22_5mybatis多表操作

    1.表之间的关系 一对多 多对一 一对一 多对多 举例: 用户和订单就是一对多 订单和用户就是多对一 一个用户可以下多个订单 多个订单属于同一个用户 人和身份证号就是一对一 一个人只能有一个身份证号 ...

  5. ASP.NET Core 基础知识(四) Startup.cs类

    ASP.NET Core应用程序需要一个启动类,按照约定命名为Startup.在 Program 类的主机生成器上调用 Build 时,将生成应用的主机, 通常通过在主机生成器上调用 WebHostB ...

  6. Linux( Ubantu ) 的简单使用

    1.Linux常用的快捷键 Ctrl + Alt+ T --打开终端 Ctrl + Shift + C --复制 Ctrl + Shift + V --粘贴 Ctrl + Shift + A --光标 ...

  7. Java-ConfigHelper工具类

    /** * 读取配置文件 */ import java.io.File; import java.net.URL; import org.apache.commons.configuration.Co ...

  8. JZOJ 5988 珂学计树题 (Burnside引理)

    什么神题a-没学过Burnside引理a学了也做不来系列-考场没怎么看这题,上最后十分钟打了样例就溜了-然后这题爆0了. Here CODE #include <cctype> #incl ...

  9. 关于.Net中Process和ProcessStartInfor的使用

    本文主要是介绍在.Net中System.Diagnostics命名空间下Process类和ProcessStartInfo类的使用 用于启动一个外部程序所使用的类是Process,至于ProcessS ...

  10. gradle——入门

    为脚本中自定义变量传参: gradle -Pmyprop=myvalue 脚本攻略 https://blog.csdn.net/yanbober/article/details/49314255 强制 ...