leetcode Trapping Rain Water pthon
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的更多相关文章
- [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 ...
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- LeetCode: Trapping Rain Water 解题报告
https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...
- 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 ...
- [leetcode]Trapping Rain Water @ Python
原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: Given n non-negative integers represe ...
- Leetcode Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] Trapping Rain Water 栈
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] Trapping Rain Water II 题解
题意 题目 思路 我一开始想的时候只考虑到一个结点周围的边界的情况,并没有考虑到边界的高度其实影响到所有的结点盛水的高度. 我们可以发现,中间是否能够盛水取决于边界是否足够高于里面的高度,所以这必然是 ...
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
随机推荐
- Cooley-Tukey算法 (蝶形算法)
Cooley-Tukey算法差别于其它FFT算法的一个重要事实就是N的因子能够随意选取.这样也就能够使用N=rS的Radix-r算法了.最流行的算法都是以r=2或r=4为基的,最简单的DFT不须要不论 ...
- SQL Server DML(SELECT)常见用法(二)
1 引言 上篇讲到SQL Server中DML的基本使用方法,其中SELECT语句是最常用的语句,其功能强大,结构复杂,下面通过例子,具体介绍其使用方法. 2 SELECT查询语句 SELECT语 ...
- SQL中DATEPART返回日期单独部分
定义和用法 DATEPART() 函数用于返回日期/时间的单独部分,比如年.月.日.小时.分钟等等. 语法 DATEPART(datepart,date) date 参数是合法的日期表达式.datep ...
- 阿里大鱼短信接口整合Tp3.2.3开发整理
阿里大鱼 http://www.alidayu.com/ 的短信接口总体是不错的,别安驹个人认为不管是从性价比还是稳定性上都是跟同类的短信接口好些,毕竟是大公司的东西不会差到哪去.下面把之前开发的短信 ...
- 5.7 cm server-agent 会出现无法启动
异常信息如下: 离线安装cloudera-scm-agent5.7的Unable to create the pidfile问题 在离线安装Cloudera Manager启动agent出现了如下异常 ...
- 图的最短路径问题————树上奶牛(tree.cpp)
和往常一样,继续从题目引入 树上奶牛 (tree.cpp) [题目描述] 农夫John的奶牛不是住在地上而是住在树上的QWQ. 奶牛之间需要串门,不过在串门之前他们会向John询问距离的大小.可是Jo ...
- MYSQL设置字段数据过长自动截断
自动截断如下设置: windows: 修改my.ini: [mysqld] sql-mode="STRICT_TRANS_TABLES" linux: 修改/ect/mysql/m ...
- 新浪SAE搭建项目
1.新浪云平台SAE(http://sae.sina.com.cn/).注册账号 2.创建应用 3.填写应用信息 4.应用创建完成,管理应用 5.上传代码(.zip)格式
- python sys.exit()函数说明
sys.exit()函数是通过抛出异常的方式来终止进程的,也就是说如果它抛出来的异常被捕捉到了的话程序就不会退出了. #!/usr/bin/python #!coding:utf-8 import s ...
- Oracle EBS-SQL (PO-1):检查供货比例异常.sql
select distinct msr.sourcing_rule_name 名称 , msi.description ...