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 ...
随机推荐
- 什么是OAuth
什么是OAuth 如今很多网站的功能都强调彼此间的交互,因此我们需要一种简单,标准的解决方案来安全的完成应用的授权,于是,OAuth应运而生,看看官网对其的定义: An open protocol t ...
- Javascript 中的false,零值,null,undefined和空字符串对象
在Javascript中,我们经常会接触到题目中提到的这5个比较特别的对象--false.0.空字符串.null和undefined.这几个对象很容易用错,因此在使用时必须得小心. 类型检测 我们下来 ...
- JavaScript事件处理程序 学习笔记
我一直认为Javascript的特点就是在和用户交互的过程中可以进行一些操作,那么事件作为用户交互的主要部分就显得特别重要,今天先学习了JS事件处理程序的相关内容. 首先,要明白Javascript ...
- SSH框架中一些技巧、处理办法
1.使用jstree插件时,操作成功直接刷新jstree 该页面(index.jsp)本身使用iframe框架jstree在leftFrame,操作页(add_input.jsp.add_succes ...
- zendStudio安装Xdebug项目断点调试
1,首先安装xdebug插件 传送门 2,配置php.ini文件如下: [XDebug] xdebug.profiler_append = xdebug.profiler_enable = xdebu ...
- [string]字符串中几个比较难的算法和容易搞混的题目
一.两个难点算法 1.Manacher算法,线性时间求最长回文子串 2.KMP算法,字符串匹配问题,c语言中的strStr 二.几个题目 1.最长回文子串 方法:暴力,动态规划,中心扩展,manach ...
- mysql5 乱码问题解决方案
今天在写项目时碰到了mysql数据库数据乱码的问题,也从网上查了很多方法,前后折腾了两个小时才终于调整好.现在就把调整过程中碰到的一些问题记录下来: 1.项目是SSH架构,一开始我以为是调用hiber ...
- javascript获取元素结点到页面的绝对距离的方式
var div = document.getElementById('div');var p = getPos(div); function getPos(obj) { var pos = {left ...
- ArrayList--卧槽这就是源码
最近在<数据结构与算法分析(java版)>中看到了实现ArrayList的简易代码,亲自做了一下.个中的疑点还是在代码中实现了一下.其中就包括内部类Iterator对外部成员访问的问题. ...
- Tomjson - json 解析库
Tomjson - 一个"短小精悍"的 json 解析库 Tomjson,一个"短小精悍"的 json 解析库,tomjson使用Java语言编写,主要作用是把 ...