mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
添加 0042接雨水 Go语言 版本
This commit is contained in:
@ -579,6 +579,31 @@ class Solution:
|
|||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
```go
|
||||||
|
func trap(height []int) int {
|
||||||
|
var left, right, leftMax, rightMax, res int
|
||||||
|
right = len(height) - 1
|
||||||
|
for left < right {
|
||||||
|
if height[left] < height[right] {
|
||||||
|
if height[left] >= leftMax {
|
||||||
|
leftMax = height[left] // 设置左边最高柱子
|
||||||
|
} else {
|
||||||
|
res += leftMax - height[left] // //右边必定有柱子挡水,所以遇到所有值小于等于leftMax的,全部加入水池中
|
||||||
|
}
|
||||||
|
left++
|
||||||
|
} else {
|
||||||
|
if height[right] > rightMax {
|
||||||
|
rightMax = height[right] // //设置右边最高柱子
|
||||||
|
} else {
|
||||||
|
res += rightMax - height[right] // //左边必定有柱子挡水,所以,遇到所有值小于等于rightMax的,全部加入水池
|
||||||
|
}
|
||||||
|
right--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
JavaScript:
|
JavaScript:
|
||||||
```javascript
|
```javascript
|
||||||
//双指针
|
//双指针
|
||||||
|
Reference in New Issue
Block a user