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的更多相关文章

  1. [LeetCode] Trapping Rain Water II 收集雨水之二

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  2. [LeetCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  3. LeetCode: Trapping Rain Water 解题报告

    https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...

  4. Leetcode: Trapping Rain Water II

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  5. [leetcode]Trapping Rain Water @ Python

    原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: Given n non-negative integers represe ...

  6. Leetcode Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  7. [LeetCode] Trapping Rain Water 栈

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  8. [LeetCode] Trapping Rain Water II 题解

    题意 题目 思路 我一开始想的时候只考虑到一个结点周围的边界的情况,并没有考虑到边界的高度其实影响到所有的结点盛水的高度. 我们可以发现,中间是否能够盛水取决于边界是否足够高于里面的高度,所以这必然是 ...

  9. [Leetcode][Python]42: Trapping Rain Water

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...

随机推荐

  1. flexigrid 修改json格式

    1.修改默认的json格式为key:value 修改前 rows: [{id:'ZW',cell:['ZW','ZIMBABWE','Zimbabwe','ZWE','716']},{id:'ZW', ...

  2. 对拍BAT

    :loop makedata.exe K.exe Kture.exe fc a.out b.out if %errorlevel%==0 goto loop pause

  3. UVA 1660 Cable TV Network

    题意: 求一个无向图的点连通度. 分析: 把一个点拆成一个入点和一个出点,之间连一条容量为1的有向边,表示能被用一次.最大流求最小割即可.套模板就好 代码; #include <iostream ...

  4. 不用css样式表和背景图片实现圆角矩形,超简洁!

    当网站页面的整体布局设计好后,接下来有很多细节的实现是很让人头疼的.其中之一就是圆角矩形的实现. 在网上看了很多圆角矩形的实现方法,基本有两种,一种是用纯css实现,不需要背景图片:另一种是用背景图像 ...

  5. Javascript Number类型常见迷惑点

    1:NaN(Not a Number) 表示一个本来要返回数值的操作数没有返回数值的情况.在ECMAscript中,任何数除以0会返回NaN[ps:实际上只有0/0会返回NaN],正(负)数除以0会返 ...

  6. 5.7.2.2 min()和max()方法

    Math对象还包含许多方法,用于辅助完成简单和复杂的数学计算. 其中,min()和max()方法用于确定一组数值中的最小值和最大值.这两个方法都可以接受任意多个数值参数,如下例子: var max = ...

  7. eclipse中使用Lombok

    1.下载Lombok.jar http://projectlombok.googlecode.com/files/lombok.jar 2.运行Lombok.jar: java -jar  D:\00 ...

  8. javascript 数据结构和算法读书笔记 > 第三章 列表

    1. 结构分析 列表首先要有以下几个属性: listSize 长度 pos 当前位置 dataStore 数据 我们要通过以下方法对上面三个属性进行操作: length() 获取长度 | getPos ...

  9. data stage走起

    如题,希望以后可以找到相应的工作.(已经工作3年以上了)

  10. SQL Server 空间监测

    数据库文件型: select * from sys.dm_db_file_space_usage;      go                                           ...