Java for LeetCode 042 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
.
解题思路:
先找到第一块最高的木板,然后从前向最高木板遍历,接着从后向最高木板遍历,JAVA实现如下:
static public int trap(int[] height) {
int result=0,peak=0,firstMax=0;
for(int i=0;i<height.length;i++)
if(height[i]>height[firstMax])
firstMax=i;
for(int i=0;i<firstMax;i++)
if(height[i]>height[peak])
peak=i;
else result+=height[peak]-height[i];
peak=height.length-1;
for(int i=height.length-1;i>firstMax;i--)
if(height[i]>height[peak])
peak=i;
else result+=height[peak]-height[i];
return result;
}
Java for LeetCode 042 Trapping Rain Water的更多相关文章
- LeetCode 042 Trapping Rain Water
题目要求:Trapping Rain Water Given n non-negative integers representing an elevation map where the width ...
- [leetcode][042] Trapping Rain Water (Java)
我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步. 题目在这里: ...
- [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 ...
- 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 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] 407. 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
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
随机推荐
- 20.(转)Android的样式(Style)和主题(Theme)
Android上的Style分为了两个方面: 1,Theme是针对窗体级别的,改变窗体样式: 2,Style是针对窗体元素级别的,改变指定控件或者Layout的样式. Android系统的themes ...
- TYVJ1000 A+B problem [存个高精模板]
A+B Problem! 通过模拟我故乡非洲的计算方式,我们很快可以解决这道题. #include<iostream> #include<cstdio> #include< ...
- NOIP2014 day2 T2 洛谷P2296 寻找道路
题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点连通. 2 .在满足条 ...
- java 线程---成员变量与局部变量
关于成员变量与局部变量: 如果一个变量是成员变量,那么多个线程对同一个对象的成员变量进行操作时,他们对该成员变量是彼此影响的(也就是说一个线程对成员变量的改变会影响到另一个线程) . 如果一个变量是局 ...
- Windows 下配置使用MemCached(转载)
工具: memcached-1.2.6-win32-bin.zip MemCached服务端程序(for win) Memcached Manager win下的Mem ...
- 实现ajax
啊打发 <script type="text/javascript" src="js/jquery-1.8.3.min.js"></scrip ...
- ftp (文件传输协议)
ftp (文件传输协议) 锁定 本词条由“科普中国”百科科学词条编写与应用工作项目 审核 . FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议” ...
- Linux常用命令 查看进程信息时 copy的-----温故而知新
1.查进程 ps命令查找与进程相关的PID号: ps a 显示现行终端机下的所有程序,包括其他用户的程序. ps -A 显示所有程序. ps c 列出程序时,显示每个程序真正的 ...
- Ubuntu 12.04 安装 Chrome浏览器
1,先到chrome官网下载一个安装包 http://www.google.com/intl/zh-CN/chrome/ 2,ctrl+alt+t 打开终端. 3,在终端里输入sudo apt-get ...
- mysql链接数据库时报错
今天在命令行下链接mysql数据库报错,如下: ERROR (HY000): Can't connect to MySQL server on 'ost' (113) 这是一个什么玩意呢,怎么会报这个 ...