当前位置:网站首页>【LeetCode】42. 接雨水 - Go 语言题解
【LeetCode】42. 接雨水 - Go 语言题解
2022-07-30 22:48:00 【想变厉害的大白菜】
一、题目描述
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
示例 1:
输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
输出:6
解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。
示例 2:
输入:height = [4,2,0,3,2,5]
输出:9
提示:
n == height.length
1 <= n <= 2 * 104
0 <= height[i] <= 105
题目链接:https://leetcode.cn/problems/trapping-rain-water/
二、题解一
1. 思路
每一处柱子能够存储的水量取决于其左边最高的柱子和其右边最高的柱子中比较矮的那一个的高度减去其自身的高度。
那么我们创建两个数组,left_height 和 right_height 分别存储当前柱子左边的最高柱子高度和当前柱子右边的最高柱子高度。
然后取出当前位置的 left_height 和 right_height 中比较矮的那一个,减去当前位置的柱子本身的高度。
再相加就是总的水量。
2. Golang 代码
func trap(height []int) int {
left_height := []int{
}
left_max := 0
for _,i := range height{
if i > left_max{
left_max = i
left_height = append(left_height,i)
}else{
left_height = append(left_height,left_max)
}
}
fmt.Println(left_height)
right_height := make([]int,len(height))
right_max := 0
for i := len(height)-1; i>-1; i--{
if height[i] > right_max{
right_max = height[i]
right_height[i] = height[i]
}else{
right_height[i] = right_max
}
}
fmt.Println(right_height)
rain := 0
for i := 0; i<len(height); i++{
rain += min(left_height[i],right_height[i])-height[i]
}
return rain
}
func min(a,b int) int{
if a < b{
return a
}else{
return b
}
}
三、题解二:双指针
上面的方法时间复杂度和空间复杂度都比较高。(搜索三次,新增存储两个切片)
以下是使用双指针的改进方法。(只搜索一次,新增存储三个整数)
1. 思路
通过观察,我们发现:
- 在低洼处(两边高)会形成积水
- 积水高度取决于两边最高的墙壁中比较低的那一个
从 2 的直觉出发,从两边开始搜索,哪边比较低,就把哪边的指针向前移动,进行搜索,另一个不动的指针指向的是当前最高的墙壁。
同时,设置 max 记录当前最高水位,也就是正在搜索的这一边中,搜索过的最高值。
每搜索一个位置,总的水量都增加 max - height[当前位置]。
2. Golang 代码
func trap(height []int) int {
i := 0
j := len(height)-1
rain := 0
max := 0
//直到两指针相遇
for i < j {
//i这边比较矮,就将i向前移动
if height[i] <= height[j]{
//特殊判断,i处于起始位置
if i == 0 {
max = height[i]
i++
continue
}
//特殊判断,遇到更高的柱子
if height[i] > max{
max = height[i]
}
rain += max - height[i]
i++
}else{
//j这边比较矮,就将j向前移动
//特殊判断,j处于起始位置
if j == len(height)-1 {
max = height[j]
j--
continue
}
//特殊判断,遇到更高的柱子
if height[j] > max{
max = height[j]
}
rain += max - height[j]
j--
}
}
return rain
}
评判结果:

边栏推荐
- d使用among的问题
- PyTorch模型导出到ONNX文件示例(LeNet-5)
- cnpm installation steps
- MySQL连接时出现2003错误
- "Code execution cannot continue because MSVCP140.dll was not found, reinstalling the program may resolve the problem, etc." Solutions
- 【Summary】机器人方法汇总
- 2021GDCPC Guangdong University Student Programming Competition B.Byfibonacci
- MySql统计函数COUNT详解
- “蔚来杯“2022牛客暑期多校训练营4 DHKLN
- win10重建索引
猜你喜欢

EasyExcel综合课程实战

通过社交媒体建立个人IP的 5 种行之有效的策略

一文详解:SRv6 Policy模型、算路及引流
![[MySQL] Related operations on databases and tables in MySQL](/img/a5/c92e0404c6a970a62595bc7a3b68cd.gif)
[MySQL] Related operations on databases and tables in MySQL

2sk2225代换3A/1500V中文资料【PDF数据手册】

Jetson AGX Orin 平台关于c240000 I2C总线和GMSL ses地址冲突问题

【MySQL】MySQL中对数据库及表的相关操作

The most complete Redis basic + advanced project combat summary notes in history

IDEA使用技巧

Rust编译报错:error: linker `cc` not found
随机推荐
ML之shap:基于FIFA 2018 Statistics(2018年俄罗斯世界杯足球赛)球队比赛之星分类预测数据集利用RF随机森林+计算SHAP值单样本力图/依赖关系贡献图可视化实现可解释性之攻略
[MySQL] DQL related operations
IDEA使用技巧
1064 Complete Binary Search Tree
EasyExcel comprehensive course combat
for...in 和 for...of 的区别
可视化工具Netron介绍
代码越写越乱?那是因为你没用责任链
VS2017编译Tars测试工程
CISP-PTE Zhenti Demonstration
2022.7.30
MySQL 5.7 detailed download, installation and configuration tutorial
win10重建索引
$\text{ARC 145}$
IJCAI2022教程 | 口语语言理解:最新进展和新领域
“由于找不到MSVCP140.dll,无法继续执行代码,重新安装程序可能会解决此问题等”解决方案
宁波中宁典当转让29.5%股权为283.38万元,2021年所有者权益为968.75万元
【无标题】
【MySQL】DQL相关操作
通过社交媒体建立个人IP的 5 种行之有效的策略