[LeetCode]42. Trapping Rain Water雨水填坑
这个题难点在于无法保证右边是不是有更高的墙可以保证挡住水
双指针可以解决
/*
两边指针保证,保证另外一边肯定有能挡住水的地方。
如果从一边开始,不考虑另一边,是无法保证右边肯定有挡水的墙,如果右边只都比这个值小,遍历的时候是不能增加结果的
双指针每次取较小的一方开始遍历
*/
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雨水填坑的更多相关文章
- [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的五种解法详解
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(积水体积)
题目链接: https://leetcode.com/problems/trapping-rain-water/?tab=Description Problem: 根据所给数组的值,按照上图的示意 ...
随机推荐
- Python机器学习笔记:异常点检测算法——LOF(Local Outiler Factor)
完整代码及其数据,请移步小编的GitHub 传送门:请点击我 如果点击有误:https://github.com/LeBron-Jian/MachineLearningNote 在数据挖掘方面,经常需 ...
- Spring Cloud 学习 (八) Spring Boot Admin
Spring Boot Admin 用于管理和监控一个或者多个 Spring Boot 程序 新建 spring-boot-admin-server pom <parent> <ar ...
- cJSON的使用
1 安装cJSON github地址:https://github.com/DaveGamble/cJSON.git 下载完成后进入cJSON目录,执行下面命令生成Makefile文件 mkdir b ...
- 微软发布 Pylance:改善 VS Code 中的 Python 体验
原标题:微软发布 Pylance:改善 VS Code 中的 Python 体验 来源:开源中国 微软宣布推出一种新的 Python 语言服务器,名为 Pylance,其可利用语言服务器协议与 VS ...
- AlanShan数据库课程设计报告
目 录 1.绪论.... 2 1.1前言... 2 1.2社会背景... 2 1.3超市背景... 3 2.系统可行性研究.... 4 2.1 技术可行性研究... 4 2.2 经济可行性研究. ...
- Python中sorted(iterable, *, key=None, reverse=False)函数参数定义中的独立星号(*)的含义
老猿在 <Python中函数的参数带星号是什么意思?>中介绍了Python函数中参数带星号的含义,而在实际使用和Python的标准文档中,会看到某写函数(如sorted(iterable, ...
- 第9.6节 Python使用read函数读取文件内容
一.语法 read(size=-1) read函数实际上在读取文本文件和二进制文件时,调用的是不同类的read,这是因为文本文件和二进制文件打开后返回的文件对象类型不同,同时读取的具体处理机制上也不同 ...
- RedHat操作指令第3篇
系统信息 arch 显示机器的处理器架构 uname -m 显示机器的处理器架构 uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS / DMI) ...
- 关于utf-8编码值 [ASIS 2019]Unicorn shop
0x00 前言 这题拿到之后有点懵,后来看了 网上的 wp 更加懵,网上大多数都是直接说 去 compart 搜thousand,然后找个大于1337 的就可以,至于为什么?基本都没有给出解答.于是乎 ...
- Alpha冲刺阶段Day4
[Alpha冲刺阶段]Scrum Meeting Daily4 1.会议简述 会议开展时间 2020/5/25 7:30-7:50 PM 会议基本内容摘要 讨论了各自任务完成情况以及明日计划 参与 ...