【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 ...
随机推荐
- Android Training Caching Bitmaps 翻译
原文:http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html 图片缓存 在Android开发中,加载一个图 ...
- Slapper帮助Dapper实现一对多
Dapper的Query的方法提供了多个泛型重载可以帮助我们实现导航属性的查询 1对1 public class Employees4List { public int Id { get; set; ...
- sshpass 配置密码登录ssh
tar -zxvf sshpass-1.06.tar.gzcd sshpass-1.06./configuremake && make install sshpass -p userp ...
- ElasticSearch入门 第六篇:复合数据类型——数组,对象和嵌套
这是ElasticSearch 2.4 版本系列的第六篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...
- Python基础爬虫
搭建环境: win10,Python3.6,pycharm,未设虚拟环境 之前写的爬虫并没有架构的思想,且不具备面向对象的特征,现在写一个基础爬虫架构,爬取百度百科,首先介绍一下基础爬虫框架的五大模块 ...
- 网络通信协议六之IP地址和MAC地址特征分析
逻辑地址和物理地址 >>逻辑地址:工作在网络层,也叫IP地址,①具有全局唯一性②用软件实现③32位 10.1.0.6 -——>00001010.00000001.00000000.0 ...
- ifnull与nvl
mysql 用 ifnull ,oracle没有ifnull 但是有相应的替换函数 nvl NVL(eExpression1, eExpression2)
- 查看dsym的报错信息(友盟)
//搜索显示报错信息的命令在终端直接运行(来源老版提示) export dSYMPath="$(find ~/Library/Developer/Xcode -iname '*.dSYM' ...
- git add详解
git add . :他会监控工作区的状态树,使用它会把工作时的所有变化提交到暂存区,包括文件内容修改(modified)以及新文件(new),但不包括被删除的文件. git add -u :他仅监控 ...
- PHP 正则表达式---匹配模式
1.PHP 正则表达式 正则表达式贪婪匹配,非贪婪匹配. 默认是贪婪匹配, 例如 ①.贪婪匹配, $str = ' 香肠 月饼 '; preg_match('/ (.)</td>/',$s ...