【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 ...
随机推荐
- 3.STM32F4按键扫描函数
//按键处理函数 //返回按键值 //mode:0,不支持连续按;1,支持连续按; //0,没有任何按键按下 //1, KEY0 按下 2, KEY1 按下 3, KEY2 按下 4, WKUP 按下 ...
- Missing artifact com.h2database:h2:jar:1.4.197
之前OK的项目再次打开pom上报错: 一起出现的现象: maven库中这个包和H2数据库的包每次项目右键→maven→update project都会产生.lastupdate文件.原来是以前从mav ...
- process 多进程写法 multiprocessing
from multiprocessing import Process def f1(n):#普通 print(f1) if __name__ == '__main__': lst = [] fo ...
- [No000010A]Git3/9-创建版本库
什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改.删除,Git都能跟踪,以便任何时刻都可以追踪历史,或 ...
- Scss基础用法
Scss基础用法 一.注释用法: (1)//comment:该注释只是在.scss源文件中有,编译后的css文件中没有. (2)/! /:重要注释,任何style的css文件中都会有,一般放置css文 ...
- linux 记录所有用户bash操作日志
记录所有用户登录系统的任何操作日志,以便有据可查. 1.编辑 /etc/profile文件. 1 # vim /etc/profil 2. 在其后添加如下内容 1 2 3 4 5 6 7 8 ...
- 用NFS挂载root出现:NFS: failed to create MNT RPC client, status=-101(-110)
2014-02-18 08:06:17 By Ly #Linux 阅读(78) 评论(0) 错误信息如下: Root-NFS: nfsroot=/home/zenki/nfs/rootfs NFS ...
- 分布式锁 AP需求 CP需求
小结: 1. 2019给Java程序员的唯一1条建议 https://mp.weixin.qq.com/s/dpx4GsGgZ0xtvzKd5riJng
- PHP-之POSIX系列函数和兼容Perl系列函数比较
PHP有两种正则系列函数 POSIX 系列和兼容Perl系列的函数 在PHP大于5.3使用POSIX系列函数会报E_DEPRECATED 错误, POSIX系列函数在大于5.3版本不建议使用,PHP7 ...
- iOS开发使用pdf切图
把pdf资源拖到Assets.xcassets里面, 打开最右边的按钮, scales选择single scale就可以像以前一样使用了: [UIImage imageName:@"xxx ...