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. 前端学习——JQuery Ajax使用经验

    0.前言     在项目推进过程中常常使用Ajax,通过Jquery提供的函数能够很方便的使用Ajax,可是在实际使用中也遇到一些问题,比如怎样防止浏览器使用缓存,怎样使用同步方式等.通过博文整理总结 ...

  2. 获取oracle sql语句中绑定变量值的方法

    在诊断 sql的性能问题时,我们有时候须要获取其绑定变量的实际值,然后将此实际值带入到sql语句其中,用原来的sql构成select语句(带where条件),实际的运行一下,看一下选择性怎样. 本文就 ...

  3. js 去重 字符串 [123123,123123,345435,33467,45645,343467,879,45645]

    function unique(dislodgeArr) { var ret = [] var hash = {} var datasource = new Array(); var array= d ...

  4. 【衡阳八中noip模拟题】国色天香

    庭前芍药妖无格,池上芙蕖净少情.唯有牡丹真国色,花开时节动京城.——唐·刘禹锡<赏牡丹>芍药花再红终究妖艳无格.终不及牡丹,国色天香.——乌拉那拉氏宜修华妃总是想要用自己的气焰打压皇后,正 ...

  5. (Qt 翻译) QGLSceneNode

    #include <QGLSceneNode> QGLSceneNode ( QObject * parent = 0 ) QGLSceneNode ( const QGeometryDa ...

  6. 这是第二道题内容要求写一个银行的ATM系统 这个浪费了好长时间 ,遇到了许多问题,不过都解决了,上程序

    下面的4个用户是我宿舍的,当然我是钱最多的,呵呵! #include<iostream>#include<string>using namespace std; class c ...

  7. XML字符串转化json

    public static String XMLTOJSON(String XMLContent,String tag) {  XMLSerializer xmlSerializer = new XM ...

  8. Android周笔记(9.8-14)(持续更新)

    本笔记记录一周内的小知识点和一些心学习的Demo. 1.PopupWindow: new 一个activity_pop_window:id为popwindow的Button,id为hello123的T ...

  9. 互联网大公司的CEO,多是程序员出身

    互联网有个现象,大公司的CEO,多是程序员出身.举例如下:------马化腾93年深大计算机系毕业,进入润迅通信从软件工程师做到开发部主管,98年11月与张志东等凑齐50万元注册腾讯公司,99年2月开 ...

  10. include的简单使用

    1.事前准备 <!--在res/values/styles.xml中--> <!--设置样式--> <style name="RemoteButton" ...