[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: 根据所给数组的值,按照上图的示意 ...
随机推荐
- Spring Cloud Alibaba 初体验(一) Nacos 配置中心
一.Nacos 下载与初始化配置 本文使用1.2.0,下载地址:https://github.com/alibaba/nacos/releases Nacos 单机模式支持持久化配置到 MySQL 数 ...
- 建立sa用户登录
Sa用户是SQL Server的超级管理员用户,由于该用户的特殊性,往往容易成为被攻击的漏洞对象,因此建议不要轻易启动该用户.下面讲述如何以Sa用户的身份登录当前的SQL Server系统. 第一步: ...
- web网络漏洞扫描器编写
这两天看了很多web漏洞扫描器编写的文章,比如W12scan以及其前身W8scan,还有猪猪侠的自动化攻击背景下的过去.现在与未来,以及网上很多优秀的扫描器和博客,除了之前写了一部分的静湖ABC段扫描 ...
- ASP.NET Log4net数据库日志新增记录客户端ip
LOG4数据库记录器XML配置 1 <appender name="ADONetAppender" type="log4net.Appender.ADONetApp ...
- 差分约束系统——POJ1275
之前做过差分,但是没做过差分约束系统. 正好在学军机房听课讲到这道题,就顺带学了一下. 其实...就是列不等式组然后建图 作为蒟蒻,当然是不会加二分优化的啦...但是poj上还是94ms跑过了qwq ...
- VS调试相关
1.查看DataSet的数据
- python 使用UUID库生成唯一ID
首先导包: import uuid uuid1(): # make a UUID based on the host ID and current time # 基于MAC地址,时间 ...
- 在Qt中配置海康工业相机SDK及遇到的问题(报错)
1.在项目的.pro文件里导入海康工业相机的SDK路径 INCLUDEPATH += \ D:\HKVersion\MVS_3.1.0\MVS\Development\Includes #这时到入Op ...
- 容器编排系统之Kubectl工具的基础使用
前文我们了解了k8s的架构和基本的工作过程以及测试环境的k8s集群部署,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/14126750.html:今天我们主要来 ...
- JWT 注册登录
1.JWT安装配置 pip install djangorestframework-jwt==1.11.0 1.2 syl/settings.py 配置jwt载荷中的有效期设置 # jwt载荷中的有效 ...