LeetCode(42)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.
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!
分析
AC代码
class Solution {
public:
int trap(vector<int>& height) {
if (height.empty())
return 0;
int len = height.size();
int lhs = 0, rhs = len - 1, secHeight = 0;
int area = 0;
//从两边向中间统计,分别统计每个竖立格子可以盛的水量
while (lhs < rhs)
{
if (height[lhs] < height[rhs])
{
secHeight = max(height[lhs], secHeight);
//加上lhs竖格的盛水量
area += secHeight - height[lhs];
//左侧右移一格
lhs++;
}
else{
secHeight = max(height[rhs], secHeight);
//加上rhs竖格的盛水量
area += secHeight - height[rhs];
//右侧左移一格
rhs--;
}//fi
}//while
return area;
}
};
LeetCode(42)Trapping Rain Water的更多相关文章
- (算法)Trapping Rain Water II
题目: Given n * m non-negative integers representing an elevation map 2d where the area of each cell i ...
- (算法)Trapping Rain Water I
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
- leetcode 第41题 Trapping Rain Water
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
- LeetCode 笔记系列12 Trapping Rain Water [复杂的代码是错误的代码]
题目:Given n non-negative integers representing an elevation map where the width of each bar is 1, com ...
- LeetCode(42):接雨水
Hard! 题目描述: 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度 ...
- LeetCode: Trapping Rain Water 解题报告
https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
随机推荐
- wordpress数据库结构及表说明
数据表结构: wp_commentmeta:存储评论的元数据wp_comments:存储评论wp_links:存储友情链接(Blogroll)wp_options:存储WordPress系统选项和插件 ...
- python实现计数排序
计数排序有局限性,最小值和最大值决定着数组的长度,如果分配均匀的话可以用 # @File: count_sort import random def count_sort(li, max_num=10 ...
- Android课程设计第六天欢迎界面(跳转)
注意:课程设计只为完成任务,不做细节描述~ package com.example.myapplication; import android.app.Activity; import android ...
- Codeforces Round #321 (Div. 2)
水 A - Kefa and First Steps /************************************************ * Author :Running_Time ...
- VS Code 自用插件备份
自用插件备份 Auto Close Tag 自动闭合标签 Atuo Rename Tag 更改前面标签的时候, 自动更改后面的闭合标签 Guides 对齐线 open-in-browser 在浏览器中 ...
- Tomcat启动后打开页面提示404错误的解决
Eclipse配置并启动Tomcat成功,但有时会访问localhost:8080出现404错误,此时需要修改Tomcat配置.步骤如下: 在Eclipse中双击Tomcat server,打开Tom ...
- 482 License Key Formatting 注册码格式化
详见:https://leetcode.com/problems/license-key-formatting/description/ C++: class Solution { public: s ...
- ASP.NET Core Action 读取流
以前mvc5 action可以直接使用 var stream = HttpContext.Current.Request.InputStream; 读取流,在Core中有所不同,可以使用以下方式读取 ...
- Web API DataContract DataMember Serializable简单解释
首先看一下DataContract这个类契约: Web API/WCF 中类一旦标记了DataContract 属性,那么类中的属性只有被标记为DataMember属性才会被序列化,也就是说一个类的属 ...
- 新建cordova应用,插件开发教程系列(总目录)
以下几篇是连续的教程,代码也是连续的,包括如下章节: 新建cordova应用 https://www.cnblogs.com/cannel/p/11074359.html 使用cordova把h5应用 ...