From 536b3594226cc989e624cae8b2603673f3199176 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=89=E8=8E=93=E5=B0=8F=E9=A5=BC=E8=82=9D?= Date: Mon, 11 Oct 2021 18:56:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200042=E6=8E=A5=E9=9B=A8?= =?UTF-8?q?=E6=B0=B4=20Go=E8=AF=AD=E8=A8=80=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0042.接雨水.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) 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 //双指针