【一天一道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, ...
随机推荐
- ACM FatMouse' Trade
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containi ...
- 前端技术之_CSS详解第二天
前端技术之_CSS详解第二天 1.css基础选择器 html负责结构,css负责样式,js负责行为. css写在head标签里面,容器style标签. 先写选择器,然后写大括号,大括号里面是样式. & ...
- 将luarocks整合进openresty
缘由 随着功能需求的深入, openresty领域的包已经不够用了, 需要lua领域本身累积的库, 也就是luarocks. 本文讲解了windows 10桌面和ubuntu server两套系统的方 ...
- Unity角色残影特效
残影特效在网上有很多例子,比如这个,我参考着自己整合了一下,算是整合了一个比较完整且特别简单易用的出来,只需要一个脚本挂上去无需任何设定就能用. 这里只针对SkinnedMeshRenderer的网格 ...
- 23 服务音乐的启动Demo4
注意如果音乐服务和Activity在一个应用中那么将不会因为绑定的Activity销毁而关闭音乐 MainActivity.java package com.qf.day23_service_demo ...
- [sed]命令笔记
sed是linux下经常用到的工具,英文全名为stream editor. sed 在windows上的实现可以在这里找到 http://gnuwin32.sourceforge.net/packag ...
- JAVA进阶之旅(一)——增强for循环,基本数据类型的自动拆箱与装箱,享元设计模式,枚举的概述,枚举的应用,枚举的构造方法,枚举的抽象方法
JAVA进阶之旅(一)--增强for循环,基本数据类型的自动拆箱与装箱,享元设计模式,枚举的概述,枚举的应用,枚举的构造方法,枚举的抽象方法 学完我们的java之旅,其实收获还是很多的,但是依然还有很 ...
- BeanUtils Exception 之 FastHashMap
这里仅仅是为了记录一件十分奇怪的事情,在使用BeanUtils的过程中,所有的依赖包都添加了, common logging common collections ··· 在为boolean 这种基本 ...
- Impala中的代码生成技术
Cloudera Impala是一种为Hadoop生态系统打造的开源MPP(massive parallel processing)数据库,它主要为分析型查询负载而设计,而非OLTP.Impala能最 ...
- 3.QT数据库综合案例,模糊查询等操作
1 新建一个项目: Database01.pro SOURCES += \ main.cpp \ Contact.cpp QT += gui widgets sql CONFIG += C++1 ...