[leetcode][042] Trapping Rain Water (Java)
我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步。
题目在这里: https://leetcode.com/problems/trapping-rain-water/
[标签] Array; Stack; Two Pointers
这个题我感觉很难,我自己是完全不会。下面贴的是别人想到的好方法,自己适当做了些简单调整。
我觉得,这个解法非常巧妙的使用了双向的 two pointers。用到的变量有左右两个指针,一个在最左边,一个在最右边,然后两个一起往中间走。在走的时候维护两个变量,left barrier 和 right barrier,即左边的挡板和右边的挡板。
用例子说明一下思路:
用leftBarrier 和 rightBarrier围成一个”盆地“。如果是leftBarrier的高度低于rightBarrier(如下图),那么固定右边一侧,尝试移动left指针,这时我们比较leftBarrier和height[left],如果是height[left]较小的话,我们知道leftBarrier 和 left之间的部分一定是可以积攒下雨水的,(图中红色标注的部分);如果是height[left]较大的话,那就更新leftBarrier。
为什么上面是固定右边一侧的rightBarrier呢?因为当rightBarrier > leftBarrier的时候,如果再遇到height[left] < leftBarrier的情形的话,那么leftBarrier 和 height[left] 之间形成的区域是可以积攒雨水的。
啊,说的好像很绕,但是道理就是这么个道理……看看如果以后有更好的解释方法再来更新吧……汗
public class Solution {
public int trap(int[] height) { int left = 0;
int right = height.length - 1; int leftBarrier = 0;
int rightBarrier = 0;
int result = 0; while (left <= right) {
if (leftBarrier <= rightBarrier) {
// there could be a basin between leftBarrier and rightBarrier
// and left side is lower one
if (height[left] > leftBarrier) {
// update left barrier
leftBarrier = height[left];
} else {
// trap water (leftBarrier - height[left]) * 1
result += leftBarrier - height[left];
}
left++;
} else {
if (height[right] > rightBarrier) {
// update right barrier
rightBarrier = height[right];
} else {
// trap water (rightBarrier - height[right]) * 1
result += rightBarrier - height[right];
}
right--;
}
}
return result;
}
}
[leetcode][042] Trapping Rain Water (Java)的更多相关文章
- LeetCode 042 Trapping Rain Water
题目要求:Trapping Rain Water Given n non-negative integers representing an elevation map where the width ...
- Java for LeetCode 042 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 收集雨水 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 ------------------------------------------------------------- ...
随机推荐
- 学c语言做练习
/*编写一个函数,其功能是使输入字符串反序.在一个使用循环语句为这个函数提供输入的完整 程序中进行测试.*/ #include<stdio.h> #include<string.h& ...
- poj 3232 Accelerator
http://poj.org/problem?id=3232 题意:有一个含有n辆车的车队,当前距离终点的距离已知,有m个加速器,每个加速器在一个时刻只能给一辆车用,一旦使用就会使得其速度由1变成k, ...
- python的工作记录A
马上进入工作自动化: [root@localhost ~]# cat svn_bbs.py import os,sys,commands,subprocess import re,time svnUr ...
- 10 001st prime number
这真是一个耗CPU的运算,怪不得现在因式分解和素数查找现在都用于加密运算. By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13 ...
- Activity的各种功能封装
Activity全屏.隐藏系统标题栏.设置屏幕方向.Activity跳转等功能 /** * Copyright (C) 2012 TookitForAndroid Project * * Licens ...
- 关于百度地图InfoWindow响应自定义布局点击事件
大概讲解: 在百度地图上显示一个marker,当marker被点击后,显示自定义的View.当自定义的View被点击后,响应不同Button的点击事件.被百度这个infowindo里面的view坑惨了 ...
- ASP.NET MVC framework 学习
http://www.cnblogs.com/lmfeng/archive/2013/03/28/2986123.html MVC数据绑定方式 http://www.cnblogs.com/lmfe ...
- HAVING 子句 (SQL Server Compact)
MSDN官方文献 原文地址:http://technet.microsoft.com/zh-cn/library/ms173260.aspx
- LeetCode-Word LadderII
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- Hadoop2.4.1 64-Bit QJM HA and YARN HA + Zookeeper-3.4.6 + Hbase-0.98.8-hadoop2-bin HA Install
Hadoop2.4.1 64-Bit QJM HA and YARN HA Install + Zookeeper-3.4.6 + Hbase-0.98.8-hadoop2-bin HA(Hadoop ...