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, ...
随机推荐
- Intellij MyBatisPlus Plugin插件破解
1. 下载原始的MyBatisPlus Plugin插件. 2. 下载替换包,请根据实际版本下载: https://github.com/myoss/profile/tree/master/idea/ ...
- #define中的特殊符号
(关于#define的用法,看这里 http://www.cppblog.com/kenny/archive/2011/04/26/145087.html) #define Conn(x,y) x## ...
- 决策树之Cart算法一
Contents 1. CART算法的认识 2. CART算法的原理 3. CART算法的实现 1. CART算法的认识 Classification And Regression ...
- Android 获取内存信息
由于工作需要,研究了一下android上获取内存信息的方法,总结如下: 1.SDK获取 在Java层利用API获取很简单,直接使用ActivityManager.MemoryInfo类即可,代码如下: ...
- XCODE 添加不同IOS版本的模拟器
一.XCode->Preferences->Downloads, 可以下载模拟器.
- Android使用genymotion模拟器
做android开发这么久了,最头痛的就是默认的模拟器慢的像蜗牛一样,实在是受够了.以前知道有genymotion存在,但是一直没有去用,今天下来用了下,哇,速度超快啊,还在用默认模拟器的小伙伴快来吧 ...
- 安卓开发笔记——打造万能适配器(Adapter)
为什么要打造万能适配器? 在安卓开发中,用到ListView和GridView的地方实在是太多了,系统默认给我们提供的适配器(ArrayAdapter,SimpleAdapter)经常不能满足我们的需 ...
- 设置回车的默认按钮detectEnter
场景: 页面有一个搜索文本框和搜索按钮.正常情况下,当我在搜索文本框输入关键字后按回车键就可以触发搜索按钮进行内容搜索,但由于页面上还有其它按钮,而且默认不是搜索按钮,怎样才能实现回车就触发我们的搜索 ...
- mongodb的一些性能管理工具
1.db.serverStatus()//查看实例运行状态,例如查看当前的连接数之类的 2. mms云平台 //由10gen提供的云管理平台//mms=mongo manage service htt ...
- GCD (Grand Central Dispatch) 笔记
GCD (Grand Central Dispatch) 是Apple公司开发的一种技术,它旨在优化多核环境中的并发操作并取代传统多线程的编程模式. 在Mac OS X 10.6和IOS 4.0之后开 ...