diff --git a/problems/0042.接雨水.md b/problems/0042.接雨水.md index b75865d7..9b26bc6b 100644 --- a/problems/0042.接雨水.md +++ b/problems/0042.接雨水.md @@ -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 //双指针