LeetCode 42 Trapping Rain Water(积水体积)

package leetcode_50; /***
*
* @author pengfei_zheng
* 求积水的体积
*/
public class Solution42 {
public static int trap(int[] height) {
if (height.length <= 2) return 0;
int max = -1;
int maxIndex = 0;
for (int i = 0; i < height.length; i++) {
if (height[i] > max) {
max = height[i];
maxIndex = i;
}
} int leftMax = height[0];
int water = 0;
for (int i = 1; i < maxIndex; i++) {
if (height[i] > leftMax) {
leftMax = height[i];
} else {
water += leftMax - height[i];
}
} int rightMax = height[height.length - 1];
for (int i= height.length - 2; i > maxIndex; i--) {
if (height[i] > rightMax) {
rightMax = height[i];
} else {
water += rightMax - height[i];
}
} return water;
}
public static void main(String[]args){
// int []height={0,1,0,2,1,0,1,3,2,1,2,1};
int []height={10,0,11,0,10};
System.out.println(trap(height));
}
}
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 ...
- Java [Leetcode 42]Trapping Rain Water
题目描述: Given n non-negative integers representing an elevation map where the width of each bar is 1, ...
随机推荐
- EventBus的思路和一些反思
本文版权归博客园和作者吴双本人共同所有 转载和爬虫请注明原文地址 www.cnblogs.com/tdws C#本地实现的和Redis Set实现的,实际上都是要维护一个Events和Handle ...
- 编译并使用带有OpenCL模块的OpenCV for android SDK
OpenCV Android SDK中提供的静态.动态库是不支持OpenCL加速的,如果在程序中调用OpenCL相关函数,编译时不会报错,但运行时logcat会输出如下信息,提示OpenCL函数不可用 ...
- ClientScript.GetCallbackEventReference实现局部刷新
使用ClientScript.GetCallbackEventReference实现局部刷新是.NET支持的一种前后台代码调用的方式:其实实现局部刷新这样方式有很多种,最经典也常用的莫过于jQuery ...
- SQL2005数据库置疑处理
2005中遇到置疑.丢失日志时按照网上常见的MSSQL2000修复方法来做, 结果发现行不通,甚至连一步都做不下去.其实,在MSSQL2005在处理置疑问题的思 路与MSSQL2000是一致的,但具体 ...
- WebGL 进入三维世界
1.观察目标点和上方向 为了确定观察者的状态,你需要获取两项信息:视点,即观察者的位置:观察目标点(look-at point),即被观察目标所在的点,它可以用来确定视线.此外,因为我们需要把观察到的 ...
- 【nginx】nginx tomcat session 共享配置
tomcat,redis下载忽略. 一.从github上下载源码,https://github.com/jcoleman/tomcat-redis-session-manager, 将源码复制到开发工 ...
- python--文件I/O--11
原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 一.打印到屏幕 最简单的 ...
- Xcode文件被锁定:The file ".xcodeproj" could not be unlocked
同事从svn上面checkout项目到本地,通过xcode打开的时候提示的这个问题. The file "xcodeproj" could not be unlocked. Cou ...
- vuejs时间格式化
date.js export function formatDate(date, fmt) { if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, ...
- iOS 优秀文章网址收录
1. iOS应用支持IPV6,就那点事儿 地址:http://www.jianshu.com/p/a6bab07c4062 2. iOS配置IPV6网络 地址:http://www.jianshu.c ...