[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 ... 
随机推荐
- databales详解(一)
			学习可参考:http://www.guoxk.com/node/jquery-datatables http://yuemeiqing2008-163-com.iteye.com/blog/20069 ... 
- 福大软工1816:Alpha(10/10)
			Alpha 冲刺 (10/10) 队名:第三视角 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务: 文字/口头描述: 1.和愈明.韫月一起对接 2 ... 
- .getClass()和.class的区别
			一直在想.class和.getClass()的区别,思索良久,有点思绪,然后有网上搜了搜,找到了如下的一篇文章,与大家分享. 原来为就是涉及到java的反射----- Java反射学习 所谓反射,可以 ... 
- Java异常(Exception)
			Java异常:运行期出现的错误 1. Java异常是Java提供的用于处理程序中错误的一种机制: 2. 错误指的是程序运行期间发生的异常事件,如除零溢出.数组下标越界.读取的文件不存在.... 3. ... 
- iOS开发应用程序生命周期
			各个程序运行状态时代理的回调: - (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSD ... 
- OSG学习:响应键盘鼠标示例
			示例功能:示例里面有两个模型,一个是牛,另一个是飞机.鼠标右键时牛和飞机都隐藏,鼠标左键双击时牛和飞机都显示,按键盘上面的LEFT键,显示牛,按键盘上面的RIGHT键显示飞机.其中显示与隐藏节点使用的 ... 
- .net平台借助第三方推送服务在推送Android,IOS消息(极光推送_V3版本)最新
			最近刚从极光推送官网上看到V2版本要停用,不得不有重新写V3版本的.这里用到了 HTTP Basic Authentication http://www.cnblogs.com/pingming/p/ ... 
- s3c2440调试nandflash裸机程序遇到的问题
			图挂了可以去 https://github.com/tanghammer/mini2440_peripherals/blob/master/nand/debug_nand.md 按照前面sdram的代 ... 
- 某一线互联网公司前端面试题js部分总结
			js部分 1,使用严格模式的优点 - 消除Javascript语法的一些不合理.不严谨之处,减少一些怪异行为; - 消除代码运行的一些不安全之处,保证代码运行的安全: - 提高编译器效率,增加运行速度 ... 
- 【Spring.Net】- 环境搭建
			参考文章:http://www.cnblogs.com/GoodHelper/archive/2009/10/25/SpringNET_Config.html 一.环境下载及安装 到Spring的官方 ... 
