题目
分析
当前的接雨水的数量 等于 min(左边最高的柱子,右边最高的柱子)。
python代码
class Solution(object):
def trap(self, height):
"""
:type height: List[int]
:rtype: int
"""
if not height: return 0
l =0
r = len(height)-1
lmax = height[l]
rmax = height[r]
res = 0
while l < r :
lmax = max(height[l],lmax)
rmax = max(height[r],rmax)
if rmax>lmax:
res+= lmax-height[l]
l+=1
else:
res+= rmax-height[r]
r-=1
return res