LeetCode 42. 接雨水(Trapping Rain Water)
题目描述
给定 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)的更多相关文章
- [Swift]LeetCode42. 接雨水 | Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- leetcode 第41题 Trapping Rain Water
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
- LeetCode 笔记系列12 Trapping Rain Water [复杂的代码是错误的代码]
题目:Given n non-negative integers representing an elevation map where the width of each bar is 1, com ...
- LeetCode: Trapping Rain Water 解题报告
https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...
- [LeetCode] 42. Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [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 ...
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
随机推荐
- Golang新开发者要注意的陷阱和常见错误
转自:http://colobu.com/2015/09/07/gotchas-and-common-mistakes-in-go-golang/ 目录 [−] 初级 开大括号不能放在单独的一行 未使 ...
- Qt5配置winpCap
在网上查了很多资料,搞了差不多一天总算解决Qt5使用winPcap配置的问题了!记录一下 以便后续忘记 1.下载winpcap4.1.3,百度即可搜索到 2.下载winpCap开发者工具包http:/ ...
- kali安装开启ssh & 安装docker
ssh相关 一.配置SSH参数 . 修改sshd_config文件,命令为: vi /etc/ssh/sshd_config 将#PasswordAuthentication no的注释去掉,并且将N ...
- Samba set of user authentication and file access rights
This series is compatible with Linux certification exam LPIC. A typical Linux user-level topics omit ...
- Delphi MSCom安装
- Linux根文件系统和目录结构及bash特性2
Linux系统上的文件类型: -:常规文件,即f d:directory,目录文件 b:block device,块设备文件,支持以“block”为单位进行随机访问 c:cha ...
- 白话跨域CORS
跨域访问控制是浏览器和服务器按照约定,协同工作,守护安全的一种机制. 其中认为浏览器和服务器是安全的,但是浏览器上运行的页面(HTML+JS)可能不安全. 分几种不同方式. 页面跨域简单请求(Get/ ...
- Vue错误集
1.Component template should contain exactly one root element. If you are using v-if on multiple elem ...
- Blend Visual studio 和Visual studio 的区别
blend for visual studio 这个并不是用于开发中写代码的,而是专门用来做WPF的界面设计的.这是为了让专业的人做专业的事,尽量让写代码的人只使用visual studio,而做界面 ...
- UVa156 Ananagrams(映射map)
Ananagrams 题目 Most crossword puzzle fans are used to anagrams--groups of words with the same letters ...