博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode Trapping Rain Water
阅读量:4108 次
发布时间:2019-05-25

本文共 986 字,大约阅读时间需要 3 分钟。

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!

很有意思的一道题,借鉴大神的思路:用两个数组分别记录某点左边最大高度,右边最大高度,然后取最小值,最后求出最小值与该点的实际高度只差,若大于0则原来计算的储水量增加该差值,否则,不增加。

int trap(int* height, int heightSize) {    if(height==NULL || heightSize<1)return 0;      int leftMax[1000];    int rightMax[1000];    int max = 0;    int water=0;    int i;    for(i = 0;i
height[i]?max:height[i]; } max = 0; for(int j =heightSize -1;j>=0;j--){ rightMax[j] = max; max = max>height[j]?max:height[j]; } for(i=0;i
rightMax[i]?rightMax[i]:leftMax[i]; int h = min - height[i]; if(h>0) water+=h; } return water;}

转载地址:http://ljtsi.baihongyu.com/

你可能感兴趣的文章
JAVA八大经典书籍,你看过几本?
查看>>
《读书笔记》—–书单推荐
查看>>
【设计模式】—-(2)工厂方法模式(创建型)
查看>>
有return的情况下try catch finally的执行顺序(最有说服力的总结)
查看>>
String s1 = new String("abc"); String s2 = ("abc");
查看>>
JAVA数据类型
查看>>
Xshell 4 入门
查看>>
SoapUI-入门
查看>>
Oracle -常用命令
查看>>
JAVA技术简称
查看>>
ORACLE模糊查询优化浅谈
查看>>
2016——个人年度总结
查看>>
2017——新的开始,加油!
查看>>
【Python】学习笔记——-6.2、使用第三方模块
查看>>
【Python】学习笔记——-7.0、面向对象编程
查看>>
【Python】学习笔记——-7.1、类和实例
查看>>
【Python】学习笔记——-7.2、访问限制
查看>>
【Python】学习笔记——-7.3、继承和多态
查看>>
【Python】学习笔记——-7.4、获取对象信息
查看>>
【Python】学习笔记——-7.5、实例属性和类属性
查看>>