在北京写一题,好爽
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.
class Solution(object):
def trap(self,
height):
def fd(lefth,left,right,righth):
#print lefth,left,right,righth
if left>right:
return 0
else:
if lefth!=0
and righth!=0:
h=min([lefth,righth])
m=0
for i in
range(left,right+1):
m+=h-height[i]
return m
else:
k=height[left:right+1]
m=max(k)
mm=k.index(m)+left
return fd(lefth,left,mm-1,m)+fd(m,mm+1,right,righth)
return fd(0,0,len(height)-1,0)
希望你能看到
2017-1-25
北京
没有下雪
本文介绍了一种计算给定高度地图上降雨后积水总量的方法。通过分治递归策略,找到最高点并将其分为两部分进行计算,最终得出积水体积。文中还分享了作者的心情和日期地点。
169

被折叠的 条评论
为什么被折叠?



