Merge pull request #842 from Axehco/master

添加 0042接雨水 Go语言 版本
This commit is contained in:
程序员Carl
2021-10-14 09:50:15 +08:00
committed by GitHub

View File

@ -579,6 +579,31 @@ class Solution:
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
//双指针