[LeetCode] 接雨水,题 Trapping Rain Water
这题放上来是因为自己第一回见到这种题,觉得它好玩儿 =)
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!
class Solution {
public:
int trap(int A[], int n) {
}
};
题目不难,观察下就可以发现被水填满后的形状是先升后降的塔形,因此,先遍历一遍找到塔顶,然后分别从两边开始,往塔顶所在位置遍历,水位只会增高不会减小,且一直和最近遇到的最大高度持平,这样知道了实时水位,就可以边遍历边计算面积。
从看题目开始一共十分钟,算本菜鸟在AC率二字打头的题目中至今最快的一次。。
class Solution {
public:
int trap(int A[], int n) {
if(n <= ) return ;
int max = -, maxInd = ;
int i = ;
for(; i < n; ++i){
if(A[i] > max){
max = A[i];
maxInd = i;
}
}
int area = , root = A[];
for(i = ; i < maxInd; ++i){
if(root < A[i]) root = A[i];
else area += (root - A[i]);
}
for(i = n-, root = A[n-]; i > maxInd; --i){
if(root < A[i]) root = A[i];
else area += (root - A[i]);
}
return area;
}
};
总结:
这道题和LeetCode上 "Candy" 一题都采用了定义两个指针向中部某一点靠拢的做法(当然Candy还有更快的解,见以前的这篇),这也算是小技巧之一吧,在需要时要能第一时间想到。
[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 ...
- [Swift]LeetCode407. 接雨水 II | Trapping Rain Water II
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- LeetCode(42)Trapping Rain Water
题目 Given n non-negative integers representing an elevation map where the width of each bar is 1, com ...
- LeetCode 42. 接雨水(Trapping Rain Water)
题目描述 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况 ...
- LeetCode 笔记系列12 Trapping Rain Water [复杂的代码是错误的代码]
题目:Given n non-negative integers representing an elevation map where the width of each bar is 1, com ...
- LeetCode: Trapping Rain Water 解题报告
https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...
- [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 ...
- [LeetCode] 407. Trapping Rain Water II 收集雨水 II
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
随机推荐
- Simple Pipelined Function
SELECT * FROM TABLE(PKG_TEST.FN_DIC_DB_TAB) CREATE OR REPLACE PACKAGE PKG_TEST IS TYPE OBJ_DICDB_R ...
- 在JS中 实现不用中间变量temp 实现两个变量值得交换
1.使用加减法; var a=1; var b=2; a=a+b; b=a-b; a=a-b; 2.使用乘除法(乘除法更像是加减法向乘除运算的映射) var a=1; var b=2; a = a * ...
- 关于CString总结
前言:串操作是编程中最常用也最基本的操作之一. 做为VC程序员,无论是菜鸟或高手都曾用过CString.而且好像实际编程中很难离得开它(虽然它不是标准C++中的库).因为MFC中提供的这个类对 我们操 ...
- Swift-(OC中的enumerateObjectsUsingBlock跟Swift的enumerate区别)
OC中使用: NSArray * lists = [NSArray array]; [lists enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUI ...
- Launch Image消失时添加动画
CGSize viewSize = self.window.bounds.size; NSString *viewOrientation = @"Portrait"; //横屏请设 ...
- JSP在页面加载时调用servlet的方法
方法:先在JS里面写一个调用servlet的事件(可以利用ajax),然后利用<body>标签的onload调用这个事件. 代码如下: jsp文件代码如下: <%@ page lan ...
- 会话模型与SSO
关于会话模型其实网站已有很多帖子说明,其中有关于sessionid,cookie以及他们之间的关系,自己先了解吧 1 会话模型 会话模型是客户端和服务端交互的一种模型,会话模型友好的处理了客户端有无通 ...
- C# 执行bat文件
private void RunBat(string batPath) { Process pro = new Process(); FileInfo file = new FileInfo(batP ...
- 3dContactPointAnnotationTool开发日志(十五)
有时候拖动一个窗口的时候可能直接拖出去了那就再也拖不回来只能reset重新来过: 于是开了个类成员变量在start里记录了一下panel的位置: var lp = panel.GetCompo ...
- 【转载】Windows下Mysql5.7开启binlog步骤及注意事项
转自:https://www.cnblogs.com/wangwust/p/6433453.html 1.查看是否开启了binlog:show binary logs; 默认情况下是不开启的. 2.开 ...