当前位置:网站首页>LeetCode——42. 接雨水(双指针)
LeetCode——42. 接雨水(双指针)
2022-06-11 16:23:00 【Always--Learning】
题目描述

解题思路
接雨水是典型的双指针问题,首先定义一个左指针和一个右指针。
初始化
- 左指针指向第一个元素
- 右指针指向最后一个元素
定义一个最终要返回的和sum=0.
定义两个变量分别用来存储左边的最大值和右边的最大值。
循环条件是左指针小于右指针。
每次进入循环都更新左指针和右指针指过轨迹的最大值。
如果左指针最大值小于右指针最大值,可以接的雨水就是左指针的最大值减去当前位置的高度,然后左指针向右移动。
如果左指针最大值大于等于右指针最大值,计算右指针最大值减去当前右指针高度,然后让右指针左移动。
为了更加方便大家对这个题目的理解,我们可以向下面这张图一样,依次画出左右指针和最大值的变化,最后返回最大值。

AC代码
var trap = function(height) {
let [left, right] = [0, height.length - 1];
let [leftMax, rightMax] = [0, 0];
let sum = 0;
while (left < right) {
leftMax = Math.max(leftMax, height[left]);
rightMax = Math.max(rightMax, height[right]);
if (leftMax < rightMax) {
sum += leftMax - height[left++];
} else {
sum += rightMax - height[right--];
}
}
return sum;
};
思考
接雨水是脉脉上经常被调侃的一道题目,当然了这个题目也经常出现在牛客等面试题中,因此我们一定要搞懂这道题,双指针问题是面试常考的一类问题,解决这类题目最重要的就是想清楚左指针和右指针在什么时候移动,左指针最大值和右指针最大值的含义是什么。
边栏推荐
- leetcode417. 太平洋大西洋水流问题(中等)
- Go quick start of go language (I): the first go program
- 真香,华为主动离职也给 N+1
- Step 4 of installation in RF: an error is reported when installing the robotframework-selenium 2library
- 什么是rs邮票纸?
- Graduation project wechat applet OUC campus navigation (I) topic selection and opening report
- 2022年高处安装、维护、拆除考试模拟100题及在线模拟考试
- Ruiji takeout project (III) employee management business development
- 项目工作区创建步骤-泽众AR自动化测试工具
- Talk about data center network again
猜你喜欢
![Interview classic question: how to do the performance test? [Hangzhou multi surveyors] [Hangzhou multi surveyors \wang Sir]](/img/ea/2c5b48b08a9654b61694b93a2e7d0a.png)
Interview classic question: how to do the performance test? [Hangzhou multi surveyors] [Hangzhou multi surveyors \wang Sir]

Zhenxiang, Huawei gives n+1 for voluntary resignation
![Enterprise purchase, sales and inventory management system based on SSM framework [source code + database + design]](/img/af/b5b5a69654a28d252cc5954b5e847a.png)
Enterprise purchase, sales and inventory management system based on SSM framework [source code + database + design]

After reading the book reading methods

Memory image of various data types in C language

rf中安装的第四步:安装 robotframework-selenium2library报错

2022起重机司机(限桥式起重机)考试题模拟考试题库及模拟考试

Laravel listening mode
![[sword finger offer] 22 The penultimate node in the linked list](/img/66/630ae9762f9d87817a14cb1c96015b.png)
[sword finger offer] 22 The penultimate node in the linked list

Differences between list and set access elements
随机推荐
1267_FreeRTOS启动第一个任务接口prvPortStartFirstTask实现分析
[sword finger offer] 21 Adjust array order so that odd numbers precede even numbers
R1 Quick Open Pressure Vessel Operation test Library and Simulation Test in 2022
leetcode463. Perimeter of the island (simple)
Zhenxiang, Huawei gives n+1 for voluntary resignation
Interview high frequency algorithm question --- longest palindrome substring
Operation guide | how to select a collector on moonbeam and Moonriver
20 full knowledge maps of HD data analysis have been completed. It is strongly recommended to collect them!
Factory calibrated gravity: working principle and installation position of carbon monoxide sensor, calibration standard description
C语言各数据类型的内存映像
时序预测 | MATLAB实现RBF径向基神经网络时间序列未来多步预测
web网页设计实例作业 ——河南美食介绍(4页) web期末作业设计网页_甜品美食大学生网页设计作业成品
Will you be punished for not wearing seat belts in the back row?
Learn about Prometheus from 0 to 1
Simulated 100 questions and simulated examination for main principals of hazardous chemical business units in 2022
Cloud data management will break the island of storage and the island of team
Step 4 of installation in RF: an error is reported when installing the robotframework-selenium 2library
Streaking? Baa!
Pyqt5 enables the qplaintextedit control to support line number display
How does the taskbar under the computer display open programs