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 ------------------------------------------------------------- ...
随机推荐
- 后台管理UI推荐
目录 一.EasyUI 二.DWZ JUI 三.HUI 四.BUI 五.Ace Admin 六.Metronic 七.H+ UI 八.其它UI 九.总结 最近要做一个企业的OA系统,以前一直使用Eas ...
- python学习笔记4(对象/引用;多范式; 上下文管理器)
### Python的强大很大一部分原因在于,它提供有很多已经写好的,可以现成用的对象 21. 动态类型:对象/引用 对象和引用: 对象是储存在内存中的实体,对象名只是指向这一对象的引用(refere ...
- 38.Android之ListView简单学习(一)
android中ListView用的很普遍,今天来学习下,本篇主要以本地数据加载到listview,后面会学习从网络获取数据添加到listview. 首先改下布局文件: <?xml versio ...
- Android 使用Parcelable序列化对象
转:http://ipjmc.iteye.com/blog/1314145 Android序列化对象主要有两种方法,实现Serializable接口.或者实现Parcelable接口.实现 ...
- c中动态使用数组
#include <iostream> #include <fstream> #include<stdlib.h> #define MAXNUM 200 int I ...
- 使用Android Studio搭建Android集成开发环境
有很长一段时间没有更新博客了,最近实在是太忙了,没有时间去总结,现在终于可以有时间去总结一些Android上面的东西了,很久以前写过这篇关于使用Android Studio搭建Android集成开发环 ...
- easyUI框架之学习3--表格datagrid
@model MVCEasyUI.Models.Sale.PageResult<MVCEasyUI.Models.Sale.Order> @{ ViewBag.Title = " ...
- servlet运行流程
servlet运行流程 (2013-06-19 19:16:43) 转载▼ 首先Servlet被部署到Web容器中,当客户端发送调用这个Servlet的请求到达Web容器时,Web容器会先判 ...
- SSN 社会安全号码
SSN是 Social Security Number 的缩写,译为「社会安全号码」,为美国社会安全卡(Social Security Card)上的 9 位数字.SSN 原本目的是用于追踪个人的纳税 ...
- mysql字符串截取
mysql字符串截取 update zcat ) where lev1 is null; update zcat ) where lev2 is null; 函数: 1.从左开始截取字符串 left( ...