【LeetCode每天一题】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.

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!
Example:
Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6 思路
在面对这道题时,我们可以设置两个指针一直指向右边,一个指向左边。基础我们每次让值小的一边移动,并且保存两边中各自最高的高度。每一次都判断和各自边上的高度之差,并得到可以蓄水的容量。直到两个节点相遇。完成遍历。时间复杂度为O(n),空间复杂度为O(1)。 图示步骤
解决代码
class Solution(object):
def trap(self, nums):
"""
:type height: List[int]
:rtype: int
"""
if len(nums) < 3: # 当数组的长度小于3时,不能蓄水直接返回结果。
return 0
max_left, max_right, left, right = nums[0],nums[-1], 0, len(nums)-1 # 设置两边的最高节点值,和左右两个指针。
res_water = 0 # 蓄水量
while left < right:
if nums[left] < nums[right]: # 我们先判断当前左右两节点大小,来决定来计算那一边。小的那一边开始(如果从大的一边开始会出现错误)
if nums[left] >= max_left: # 在判断 左边节点和左边最高的节点之间的大小关系
max_left = nums[left] # 当当前的左边节点大于或者等于之前左边最高节点值时,我们重新进行赋值。
else:
res_water += max_left - nums[left] # 否则可以蓄水,加上高度差,即为蓄水量
left += 1 # 向前移动一位
else:
if nums[right] >= max_right: # 和上边的同理
max_right = nums[right]
else:
res_water += max_right - nums[right]
right -= 1
return res_water # 循环结束返回结果。
【LeetCode每天一题】Trapping Rain Water(获得雨水的容量)的更多相关文章
- leetcode 第41题 Trapping Rain Water
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
- [LeetCode] 接雨水,题 Trapping Rain Water
这题放上来是因为自己第一回见到这种题,觉得它好玩儿 =) Trapping Rain Water Given n non-negative integers representing an eleva ...
- [LeetCode] 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 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LintCode] 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 接雨水 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 保存左右最大值 单调栈 日期 题目地址:ht ...
- LeetCode(42)Trapping Rain Water
题目 Given n non-negative integers representing an elevation map where the width of each bar is 1, com ...
- LeetCode 笔记系列12 Trapping Rain Water [复杂的代码是错误的代码]
题目:Given n non-negative integers representing an elevation map where the width of each bar is 1, com ...
- 042 Trapping Rain Water 接雨水
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算下雨之后能接多少雨水.例如,输入 [0,1,0,2,1,0,1,3,2,1,2,1],返回 6. 详见:https://leetcode.c ...
随机推荐
- ros查看摄像头是否打开正常
使用rqt_image_view命令,查看摄像头是否正常输出图像
- arcgis10.4 server第一次发布地图报错:We were unable to connect to...Error:Proxy server got bad address...
arcgis 10.4发布地图跟10.2不一样.server url里的http要改为https,否则就会连接不上.
- php之运算符
运算符优先级相同,运算符的结合方向决定了该如何运算, 一.运算符优先级 组合方向 运算符 附加信息 无 clone new clone和new 左 [ array 右 ** 算术运算符 右 ++ -- ...
- static的含义
static的含义:(1)设置变量的存储域,函数体内static变量的作用范围为该函数体,不同于auto变量,该变量的内存只被分配一次,因此其值在下次调用时仍持上次的值:(2)限制变量的作用域,在模块 ...
- PyQt5学习笔记----标准文件打开保存框QFileDialog
单个文件打开 QFileDialog.getOpenFileName()多个文件打开 QFileDialog.getOpenFileNames() 文件夹选取 QFileDialog.getE ...
- MySQL+InnoDB semi-consitent read原理及实现分析(转)
add by zhj: 主要讲的是在MySQL在Repeatable Read和Read Committed级别下,加锁时的不同,在Read Committed隔离级别下,只对where 中满足条件的 ...
- ansible源码安装
一.升级python 笔者系统为centos6.5,系统默认安装python2.6,虽然ansible官方文档要求python版本为2.6或2.7,然而许多人都说使用2.6可能出现一系列问题,所以作者 ...
- PXE安装操作系统
TFTP服务 用PXE安装操作系统依赖于DHCP服务和TFTP服务 网卡一般都内置的TFTP客户端的程序 systemctl enable tftp systemctl enable dhc ...
- 【SVM、决策树、adaboost、LR对比】
一.SVM 1.应用场景: 文本和图像分类. 2.优点: 分类效果好:有效处理高维空间的数据:无局部最小值问题:不易过拟合(模型中含有L2正则项): 3.缺点: 样本数据量较大需要较长训练时间:噪声不 ...
- 【adaboost】周志华
