这个题难点在于无法保证右边是不是有更高的墙可以保证挡住水

双指针可以解决

/*
两边指针保证,保证另外一边肯定有能挡住水的地方。
如果从一边开始,不考虑另一边,是无法保证右边肯定有挡水的墙,如果右边只都比这个值小,遍历的时候是不能增加结果的
双指针每次取较小的一方开始遍历
*/
public int trap(int[] height) {
if (height.length<3) return 0;
int l = 0;
int r = height.length-1;
int res = 0;
while (l<r)
{
int min = Math.min(height[l],height[r]);
if (min==height[l])
{
//从左边开始遍历,遇到更高的就退出
while (++l<r&&height[l]<=min)
res+=min-height[l];
}
else
{
while (l<--r&&height[r]<=min)
res+=min-height[r];
}
}
return res;
}

[LeetCode]42. Trapping Rain Water雨水填坑的更多相关文章

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

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

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

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

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

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

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

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

  5. LeetCode - 42. Trapping Rain Water

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

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

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

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

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

  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. 推荐系统实践 0x09 基于图的模型

    用户行为数据的二分图表示 用户的购买行为很容易可以用二分图(二部图)来表示.并且利用图的算法进行推荐.基于邻域的模型也可以成为基于图的模型,因为基于邻域的模型都是基于图的模型的简单情况.我们可以用二元 ...

  2. 断点调试 breakpoints(修改request)

    目录 1.抓取信息 2.点击breakpoints勾选断点 3.复制抓取的信息 4.点击proxy再点断点设置 5.点击勾选然后add添加其下如图 6.点击确定 7.刷新百度,charles出现的页面 ...

  3. LeetCode 018 4Sum

    题目描述:4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c ...

  4. 第7.6节 Python中类的继承机制详述

    在本章第一节,介绍了面向对象程序设计的三个特征:封装.继承和多态,前面章节重点介绍了封装和多态,由于Python语言是多态语言,对象的类型不再由继承等方式决定,而由实际运行时所表现出的具体行为来决定, ...

  5. B站自动爬取器并制作词云

    效果 词云展示 弹幕展示 爬取弹幕过程 基本步骤 1.寻找视频url 2.构造请求头 3.寻找弹幕地址 4.根据弹幕地址运用正则或xpath爬取 寻找B站视频的url 制作请求头 headers = ...

  6. [BJDCTF2020]Mark loves cat && [BJDCTF 2nd]简单注入 && [BJDCTF2020]The mystery of ip

    [BJDCTF2020]Mark loves cat 源码泄露 使用GitHack.py下载源码 下载之后对源代码进行审计 flag.php代码为: <?php $flag = file_get ...

  7. Linux的一些基本操作

    1. 解压,对于tar格式和zip格式不同,后者是unzip test.zip ,如果要解压到指定路径是 unzip test.zip -d ../

  8. MacOS上Parallels Desktop安装MacOSHighSierra

    下载dmg文件 http://www.macoshome.com/macos/977.html 配置宿主机时间2015-10-27 23:25 为了防止"macOS未能安装在您的电脑上_安装 ...

  9. 在IDEA上 使用maven进行打包时报错: Failed to execute goal org.apache.maven.plugins:maven-javadoc-plugin:2.10.2:jar

    报错内容: Failed to execute goal org.apache.maven.plugins:maven-javadoc-plugin:2.10.2:jar (attach-javado ...

  10. XML文件格式

    首行是需要对xml版本声明<?xml version="1.0" ecoding="UTF-8"?> 语言可根据需要修改 在编写xml的几个注意点 ...