leetcode 10

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014204761/article/details/83216584
def trap(self, height):
        """
        left_max 存左边最大的值 right存右边最大的值 
        left < right left移动 否则right移动
        """
        if not height:
            return 0
        left,right,res = 0,len(height)-1,0
        left_max,right_max =0,0
        while left < right:
            if height[left] < height[right]:
                if height[left] > left_max:
                    left_max = height[left]
                else:
                    res += left_max - height[left]
                left += 1
            else:
                if height[right] > right_max:
                    right_max = height[right]
                else:
                    res += right_max - height[right]
                right -= 1
        return res

猜你喜欢

转载自blog.csdn.net/u014204761/article/details/83216584
今日推荐