当前位置:网站首页>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;
};
思考
接雨水是脉脉上经常被调侃的一道题目,当然了这个题目也经常出现在牛客等面试题中,因此我们一定要搞懂这道题,双指针问题是面试常考的一类问题,解决这类题目最重要的就是想清楚左指针和右指针在什么时候移动,左指针最大值和右指针最大值的含义是什么。
边栏推荐
- [sword finger offer] 21 Adjust array order so that odd numbers precede even numbers
- Kill the swagger UI. This artifact is better and more efficient!
- What happened to the frequent disconnection of the computer at home
- 2022G1工业锅炉司炉考题及模拟考试
- Collection | can explain the development and common methods of machine learning!
- Interview high frequency algorithm question --- longest palindrome substring
- JDBC debugging error, ask for guidance
- 1267_FreeRTOS启动第一个任务接口prvPortStartFirstTask实现分析
- 信息收集常用工具及命令
- Customized thread communication (lock) of JUC
猜你喜欢

2022g1 industrial boiler stoker test questions and simulation test

真香,华为主动离职也给 N+1

leetcode463. Perimeter of the island (simple)

Pytest测试框架基础篇

DHCP protocol instantiation analysis

从0到1了解Prometheus

2022熔化焊接与热切割上岗证题目及模拟考试

【剑指Offer】22.链表中倒数第K节点

Cloud data management will break the island of storage and the island of team
![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]
随机推荐
[learn FPGA programming from scratch -18]: quick start chapter - operation steps 2-6- VerilogHDL sequential circuit syntax analysis (taking the counter as an example)
Cloud data management will break the island of storage and the island of team
Transfer learning
【剑指Offer】21.调整数组顺序使奇数位于偶数前面
回归预测 | MATLAB实现RBF径向基神经网络多输入单输出
Operation guide | how to select a collector on moonbeam and Moonriver
leetcode785. 判断二分图(中等)
leetcode463. 岛屿的周长(简单)
Detailed explanation of the functions of list and dict data types
09-最小生成树 公路村村通
MySQL快速入门实例篇(入内不亏)
AutoRunner自动化测试工具如何创建项目-Alltesting|泽众云测试
Implementation of VGA protocol based on FPGA
Question ac: Horse Vaulting in Chinese chess
基于文本驱动用于创建和编辑图像(附源代码)
Streaking? Baa!
2022 molten welding and thermal cutting work license and simulation examination
Graduation project wechat applet OUC campus navigation (I) topic selection and opening report
Princeton Dengjia student's personal account: must I have a doctorate? No, I can also be an applied scientist in a large factory as an undergraduate
RDKit 安装