题目描述

给定 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. javascript中对编码的解读

    首先来一下js知识的巩固与复习 js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,deco ...

  2. vue入门:(模板语法与指令)

    vuejs使用及HTML的模板语法,可以实现声明式将DOM绑定至底层VUE实例的数据.通过模板语法将数据渲染进DOM的系统,结合响应系统,在应用状态改变时,Vue能够计算出重新渲染组件的最小代价并应用 ...

  3. Python实现读取Excel文档中的配置并下载软件包

    问题:现在遇到这样一个问题,服务器存储了很多软件包,这些包输入不同的产品,每个产品都有自己的配置,互相交叉,那么到底某一产品所有配置的软件包下载后,占用多大空间呢? 分析:从这个问题入手,了解到:软件 ...

  4. python之时间日期time

    常用的时间函数如下获取当前日期:time.time()获取元组形式的时间戳:time.local(time.time())格式化日期的函数(基于元组的形式进行格式化):time.asctime(tim ...

  5. 简单的文件ftp上传

    目录 简单的文件ftp上传 简单的文件ftp上传 server import socket import struct service=socket.socket() service.bind(('1 ...

  6. Java Web系统架构概览

    大型网站系统架构的演进都是随着业务增长不断演进,所有的出发点都是为了满足业务需求.最初访问量下,功能简单时,单体软件可以解决所有问题:后来访问量逐渐增大,功能愈加丰富,此时单体软件的架构逐渐成为开发和 ...

  7. kubernetes资源清单之Deployment

    Deployment为Pod和ReplicaSets提供声明性更新 示例 --- apiVersion: apps/v1 kind: Deployment metadata:     name: de ...

  8. CentOS7 xrdp 安装和设置

    1) 安装 $ sudo yum install xrdp $ sudo yum install tigervnc $ sudo yum install tigervnc-server 2) 设置密码 ...

  9. The Preliminary Contest for ICPC Asia Nanjing 2019 B. super_log (广义欧拉降幂)

    In Complexity theory, some functions are nearly O(1)O(1), but it is greater then O(1)O(1). For examp ...

  10. iOS 环形进度条

    .h文件 #import <UIKit/UIKit.h> @interface YTProgressView : UIView@property (nonatomic, copy) NSS ...