题目描述:

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.

解题思路:

思路一:开辟两个数组空间,逐个遍历数组,找出该位置左边的最大值与右边的最大值,分别放到两个数组中。然后对整个数组进行遍历,位置装水后的值不能超过该位置左右最高值中的最小数。该算法需三次遍历数组,但是时间复杂度为O(n);空间需开辟两个数组空间,空间复杂度为O(n)。

代码如下:

public class Solution {
public int trap(int[] height) {
int length;
int maxLeftHeight = 0, maxRightHeight = 0;
int result = 0;
int temp; if (height == null || (length = height.length) == 0)
return 0;
int[] leftMaxHeight = new int[length];
int[] rightMaxHeight = new int[length]; for (int i = 0; i < length; i++) {
leftMaxHeight[i] = maxLeftHeight;
maxLeftHeight = Math.max(maxLeftHeight, height[i]);
} for (int i = length - 1; i >= 0; i--) {
rightMaxHeight[i] = maxRightHeight;
maxRightHeight = Math.max(maxRightHeight, height[i]);
} for (int i = 0; i < length; i++) {
temp = Math.min(leftMaxHeight[i], rightMaxHeight[i]);
if (temp >= height[i])
result += temp - height[i];
} return result;
}
}

思路二:

设置两个指示变量,分别存放当前指向的两个位置。找出左位置的左边的最高值和右位置的右边的最高值。对于两者中的最小值,表明当前位置加上水过后的值不超出该值,那么相减即可,反之,对另一个相减。该算法只需要一次遍历数组,所以效率更高,时间复杂度为O(n);空间方面不需要开辟数组空间,所以为常数空间。

代码如下:

public class Solution {
public int trap(int[] height) {
int length;
int left, right;
int maxLeftHeight = 0, maxRightHeight = 0;// 记录当前位置左边, 右边的最大值
int result = 0; if (height == null || (length = height.length) == 0)
return 0;
left = 0;
right = length - 1; while (left < right) {
maxLeftHeight = Math.max(maxLeftHeight, height[left]);
maxRightHeight = Math.max(maxRightHeight, height[right]); if (maxLeftHeight < maxRightHeight) {
result += maxLeftHeight - height[left];
left++;
} else {
result += maxRightHeight - height[right];
right--;
}
}
return result;
}
}

  

Java [Leetcode 42]Trapping Rain Water的更多相关文章

  1. leetcode#42 Trapping rain water的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...

  2. [array] leetcode - 42. Trapping Rain Water - Hard

    leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...

  3. LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))

    LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...

  4. LeetCode - 42. Trapping Rain Water

    42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...

  5. [LeetCode] 42. Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  6. leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

  7. [LeetCode] 42. Trapping Rain Water 解题思路

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  8. [leetcode]42. Trapping Rain Water雨水积水问题

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  9. LeetCode 42 Trapping Rain Water(积水体积)

    题目链接: https://leetcode.com/problems/trapping-rain-water/?tab=Description   Problem: 根据所给数组的值,按照上图的示意 ...

随机推荐

  1. 【dapper】.net平台下的框架

    http://www.cnblogs.com/yipu/archive/2012/11/21/2780199.html Method Duration Remarks Hand coded (usin ...

  2. IOS 透​视​投​影​矩​阵​推​导(转)

    http://wenku.baidu.com/link?url=wDkyQR9fDI_tZas1BlMRUnNNoKwiQDygltm2wWxRr_sDwcDB51_QCDfR4Gb5wYrIUZ_t ...

  3. H2嵌入式数据库的各种连接方式

    H2database是一款用java语言编写的开源数据库, 一般用作游戏的数据存储, 当然web项目也是可以用的, web项目也可以将该数据库 首先要安装H2数据库 http://www.h2data ...

  4. 集成“支付宝” -b

    大致步骤 1.与支付宝签约获取相关参数 合作者身份 ID 与安全校验码 key2.下载需要导入的文件,做相应设置3.在自己的项目中集成支付的方法代码 详细步骤 1.获取合作者身份 ID 与安全校验码 ...

  5. 1031: [JSOI2007]字符加密Cipher - BZOJ

    Description喜欢钻研问题的JS 同学,最近又迷上了对加密方法的思考.一天,他突然想出了一种他认为是终极的加密办法:把需要加密的信息排成一圈,显然,它们有很多种不同的读法.例如下图,可以读作: ...

  6. java中四种引用类型

    java中四种引用类型  今天看代码,里面有一个类java.lang.ref.SoftReference把小弟弄神了,试想一下,接触java已经有3年了哇,连lang包下面的类都不了解,怎么混.后来在 ...

  7. [转载]再谈iframe自适应高度

    Demo页面:主页面 iframe_a.html ,被包含页面 iframe_b.htm 和 iframe_c.html 下面开始讲: 通过Google搜索iframe 自适应高度,结果5W多条,搜索 ...

  8. PAT-乙级-1016. 部分A+B (15)

    1016. 部分A+B (15) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 正整数A的“DA(为1位整数)部 ...

  9. ural 1221

    本来就是个很水的题  就是枚举起点长度然后直接判断就行了   但是比赛的时候写了个大bug 还找不出来     自己太水了 #include <cstdio> #include <c ...

  10. WIN7+Ubuntu双系统,win7启动不了

    在网上搜索了下,大多说的是因为重装引起的坏道, 我经过半天的搜索才找到了问题所在,首先看看下面连接的二楼大神给出的解决方案: https://forum.ubuntu.org.cn/viewtopic ...