您的位置:首页 > 移动开发

leetcode Trapping Rain Water pthon

2015-12-24 23:18 316 查看
class Solution(object):

def trap(self,nums):

leftmosthigh = [0 for i in range(len(nums))]

leftmax=0

for i in range(len(nums)):

if nums[i] > leftmax:

leftmax=nums[i]

leftmosthigh[i] = leftmax

print leftmosthigh

sums=0

rightmax=0

for i in reversed(range(len(nums))):

print i

if nums[i] > rightmax:

rightmax = nums[i]

print 'i is',i,'rightmax is',rightmax

#current i less than it's two side, then it can trap

if min(rightmax,leftmosthigh[i] ) > nums[i]:

sums+=min(rightmax,leftmosthigh[i])-nums[i]

return sums

nums= [0,1,0,2,1,0,1,3,2,1,2,1]

obj=Solution()

rs=obj.trap(nums)

print rs


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: