【一天一道LeetCode】#42. Trapping Rain Water
一天一道LeetCode系列
(一)题目
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.
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.
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!
(二)解题
题目需要求的是矩阵作为容器能盛多少体积的水。
方法可以参考我的这篇博客:【一天一道LeetCode】#11Container With Most Water
/*
思路:考虑到所盛的水取决于容器两端中小的那一端。因此用两个指针,分别指向头和尾,依次向中间移动。
1.如果左边小,则左边向右移动一格,这个时候需要判断向右移动一格后
①如果高度大于原来的就表示盛不下水
②如果小于原来的则表示有凹下去的部分,这个时候计算高度差就代表能盛多少水。(右边比左边高,可以保证右边不溢出)
2.如果右边小,则右边向左移动一格,这个时候同1一样判断。
*/
class Solution {
public:
int trap(vector<int>& height) {
if(height.size()<=2) return 0;
int ret = 0;
int l = 0;
int r = height.size()-1;
int left = height[0];
int right = height[r];
while(l<r)
{
if(left<=right)
{
l++;
if(height[l]>=left)
{
left = height[l];
}
else ret+=(left-height[l]);
}
else
{
r--;
if(height[r]>=right)
{
right = height[r];
}
else ret +=(right-height[r]);
}
}
return ret;
}
};
【一天一道LeetCode】#42. Trapping Rain Water的更多相关文章
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
- [array] leetcode - 42. Trapping Rain Water - Hard
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
- [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] 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雨水积水问题
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- LeetCode 42 Trapping Rain Water(积水体积)
题目链接: https://leetcode.com/problems/trapping-rain-water/?tab=Description Problem: 根据所给数组的值,按照上图的示意 ...
- Java [Leetcode 42]Trapping Rain Water
题目描述: Given n non-negative integers representing an elevation map where the width of each bar is 1, ...
随机推荐
- DISC社交风格测试题--老虎 孔雀 考拉 猫头鹰
凭直觉,迅速回答 "我是谁",而不是"我应该是谁,或我想我是谁". 1.关于人生观,我的内心其实是: A 希望能够有尽量多的人生体验,所以会有非常多样化的想法. ...
- python3中替换python2中cmp函数的新函数分析(lt、le、eq、ne、ge、gt)
本文地址:http://blog.csdn.net/sushengmiyan/article/details/11332589 作者:sushengmiyan 在python2中我们经常会使用cmp函 ...
- Mybatis源码分析--关联表查询及延迟加载(一)
Mybatis提供了关联查询映射的功能. 一.一对一关联
- 安卓高级9 shareSDK 第三方登陆和分享Mob
ShareSDK推荐此官网地址 官网注册和申请key流程 注册 略 进入后台创建应用得到可以 下载SDK 解压下载的压缩包 第三方平台申请key 如果你想申请QQ登陆或者分享需要去腾讯开发者申请,申请 ...
- linux:CPU私有变量(per-CPU变量)
一.简介2.6内核上一个新的特性就是per-CPU变量.顾名思义,就是每个处理器上有此变量的一个副本.per-CPU的最大优点就是,对它的访问几乎不需要锁,因为每个CPU都在自己的副本上工作.task ...
- linux C 获取与修改IP地址
主要有两种方法: 一种是用system执行shell命令,如: system("ifconfig usb0 192.168.1.188"); 另一种用ioctl系统调用: int ...
- 这是最好的时光,这是最坏的时光 SNAPSHOT
好久没动笔了,上次憋了好几天码出的文字扔出去,石沉大海,没惊起半点涟漪.这次真不知道能憋出个什么鬼,索性就让思绪飞扬,飞到哪是哪! --题记 此处应有BGM: 少年锦时 赵雷 1.以后真没有暑假喽 2 ...
- Scala:提取器(Extractor)
http://blog.csdn.net/pipisorry/article/details/52902671 提取器是从传递给它的对象中提取出构造该对象的参数. Scala 标准库包含了一些预定义的 ...
- 21 PagerTabStrip-PagerTitleStrip-viewPager
PagerTabStrip:可以点击跳转到对应viewPager界面 PagerTitleStrip:不可点击 在eclipse开发时如果目标版本为API23那么会有不显示的问题 解决:更换v4包 解 ...
- Java基本语法-----java常量
1常量的概述 常量是指在程序运行过程中其值不能改变的量. 2常量类型 Java中常量的分类: 整数常量 : 所有整数 小数常量 : 所有小数 布尔常量 : 只有true和false 字符常量 :使用' ...