当前位置:网站首页>【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
}
评判结果:
边栏推荐
- 2022牛客暑期多校训练营1 J Serval and Essay
- ThinkPHP high imitation blue play cloud network disk system source code / docking easy payment system program
- ZZULIOJ:1120: 最值交换
- mysql获取近7天,7周,7月,7年日期,根据当前时间获取近7天,7周,7月,7年日期
- “蔚来杯“2022牛客暑期多校训练营2 H.Take the Elevator
- Mysql进阶优化篇01——四万字详解数据库性能分析工具(深入、全面、详细,收藏备用)
- Day016 Classes and Objects
- EasyExcel comprehensive course combat
- Ningbo Zhongning Pawn will transfer 29.5% of the equity for 2.8338 million yuan, and the owner's equity in 2021 will be 9.6875 million yuan
- IJCAI2022教程 | 口语语言理解:最新进展和新领域
猜你喜欢
TCP 连接 三次握手 四次挥手
IJCAI2022教程 | 口语语言理解:最新进展和新领域
2022/07/30 学习笔记 (day20) 面试题积累
WSL2设置默认启动用户(debian)
通过社交媒体建立个人IP的 5 种行之有效的策略
【无标题】
Gxlcms有声小说系统/小说听书系统源码
Compressing Deep Graph Neural Networks via Adversarial Knowledge Distillation
Py's pdpbox: a detailed introduction to pdpbox, installation, and case application
Navicat cannot connect to mysql super detailed processing method
随机推荐
$\text{ARC 145}$
482-静态库、动态库的制作、使用及区别
grub learning
Introducing the visualization tool Netron
mysql跨库关联查询(dblink)
2022 Nioke Summer Multi-School Training Camp 1 J Serval and Essay
When Navicat connects to MySQL, it pops up: 1045: Access denied for user 'root'@'localhost'
“蔚来杯“2022牛客暑期多校训练营4 L.Black Hole 垃圾计算几何
2021GDCPC Guangdong University Student Programming Competition B.Byfibonacci
通过社交媒体建立个人IP的 5 种行之有效的策略
DFS题单以及模板汇总
Learning about XML (1)
ML之shap:基于FIFA 2018 Statistics(2018年俄罗斯世界杯足球赛)球队比赛之星分类预测数据集利用RF随机森林+计算SHAP值单样本力图/依赖关系贡献图可视化实现可解释性之攻略
“蔚来杯“2022牛客暑期多校训练营2 H.Take the Elevator
CISP-PTE Zhenti Demonstration
Reverse linked list - in-place inversion method
StoneDB 为何敢称业界唯一开源的 MySQL 原生 HTAP 数据库?
连号区间数
Apache Doris系列之:安装与部署详细步骤
d违反常了吗