下面是一个使用双指针法的 Python 代码实现:
def trap(height):if not height:
return 0
n = len(height)
left, right = 0, n - 1
left_max, right_max = height[left], height[right]
result = 0
while left < right:
if height[left] < height[right]:
left += 1