码迷,mamicode.com
首页 > 移动开发 > 详细

leetcode Trapping Rain Water pthon

时间:2015-12-24 23:55:03      阅读:1432      评论:0      收藏:0      [点我收藏+]

标签:

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

 技术分享

leetcode Trapping Rain Water pthon

标签:

原文地址:http://www.cnblogs.com/allenhaozi/p/5074643.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!