题目描述:

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. Linux ps 命令获取查询结果中的单列信息

    1.查看所有进程信息,但是只想获取COMMAND列的值 SDCxM-SDCAM-root-root> ps auxUSER       PID %CPU %MEM    VSZ   RSS TT ...

  2. poj 2947 Widget Factory

    Widget Factory 题意:有n件装饰品,有m组信息.(1 <= n ,m<= 300)每组信息有开始的星期和结束的星期(是在mod 7范围内的)并且还包括num种装饰品的种类(1 ...

  3. iOS:横向使用iPhone默认的翻页效果

    大致思路使用两层辅助UIView的旋转来实现添加后的View的横向翻页效果 CATransform3D transformA = CATransform3DRotate(CATransform3DId ...

  4. ARC工程中添加非ARC文件

    转载自:http://blog.csdn.net/zhenweicao/article/details/16988543 分类: IOS2013-11-27 17:02 626人阅读 评论(0) 收藏 ...

  5. 【BZOJ 2005】[Noi2010]能量采集

    Description 栋栋有一块长方形的地,他在地上种了一种能量植物,这种植物可以采集太阳光的能量.在这些植物采集能量后,栋栋再使用一个能量汇集机器把这些植物采集到的能量汇集到一起. 栋栋的植物种得 ...

  6. python学习笔记26(python中__name__的使用)

    在python中,每个py文件都是一个模块,也都是一个可执行文件,即包含main方法.因此,对每个py文件,可以单独运行,也可以import它给其他客户使用,这两种情况不一样. 1. 如果模块是被导入 ...

  7. github简单使用

    github是一个基于git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就是代码要公开.对于一般人来说公共仓库就已经足够了,而且我们也没多少代码来管理,O(∩_∩)O ...

  8. qwt 6.1.0集成进Qt creator 2.8.1步骤

    环境准备与文件编译 ·关于Qt 发现一个Qt正确安装的教程,非常棒!http://www.wikihow.com/Install-Qt-SDK-on-Ubuntu-Linux Qt版本是4.8.5,在 ...

  9. GodSon Easyui 结合Pluplaod插件的文件分割上传

    自己整理了一个文件分割上传的实例,提供研究学习使用. 在线查看效果       下载该资源pluplaod文件分割上传Demo.zip 简介: 首先,进入页面会看到下面的效果: 点击一个按钮,出现如图 ...

  10. 1198: [HNOI2006]军机调度 - BZOJ

    Description 凯萨拥有一支由n个人组成的雇佣军,他们靠在威尼斯商行接任务过活.这支军队的成份比较复杂,不同的人往往具有不同的技能,有的人还拥有多项技能.威尼斯商行的任务也参差不齐,有的需要几 ...