42. Trapping Rain Water(直方图 存水量 hard)
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.

class Solution {
public int trap(int[] height) {
int left = 0;
int right = height.length-1;
int trap = 0;
int h = 0;
while(left<right){
if(height[left]<height[right]){
h = Math.max(height[left],h);
trap+=(h - height[left]);
left++;
}
else{
h = Math.max(height[right],h);
trap+=(h - height[right]);
right--;
}
}
return trap;
}
}
42. Trapping Rain Water(直方图 存水量 hard)的更多相关文章
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
- [array] leetcode - 42. Trapping Rain Water - Hard
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
- 刷题42. Trapping Rain Water
一.题目说明 题目是42. Trapping Rain Water,翻译起来就是"接雨水".给n个非负正数代表高度,每个正数宽度为1,让计算能多少雨水.题目难度是Hard 二.我的 ...
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
- [LeetCode] 42. Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 【LeetCode】42. Trapping Rain Water
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
随机推荐
- ubuntu 16.04安装 navicat
原文地址:http://www.cnblogs.com/wbJson/p/5655537.html 下载地址:http://download2.navicat.com/download/navicat ...
- MySQL单列索引和组合索引的选择效率与explain分析
一.先阐述下单列索引和组合索引的概念: 单列索引:即一个索引只包含单个列,一个表可以有多个单列索引,但这不是组合索引. 组合索引:即一个索包含多个列. 如果我们的查询where条件只有一个,我们完全可 ...
- Unity中用触摸控制物体旋转和放大
using UnityEngine;using System.Collections;using System.IO; public class ScaleAndRotate : MonoBehavi ...
- VC++:ActiveX Test Container
VC++6.0安装后包含了ActiveX Test Container工具,位置为: "C:\Program Files (x86)\Microsoft Visual Studio\Comm ...
- python入门(九):网络编程和多线程
一.网络编程 Socket简介 Socket又称"套接字",应用程序通常通过"套接字"向网络发出请求或者应答网络请求,使主机间或者一台计算机上的进程间可以通讯. ...
- php做图片上传功能
今天来做一个图片上传功能的插件,首先做一个html文件:text.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transition ...
- iOS 判断某一日期是否在一日期区间
-(BOOL)date:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endDate { if ([date comp ...
- 获取ScrollView的onScrollListener
scrollView.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() { @Overrid ...
- cx_Oracle在sublime text里运行遇到 ImportError错误解决办法
如果你装完cx_Oracle之后,命令行运行没错,但是在sublime text里运行, 就遇到这个错误: ImportError: dlopen(/Library/Python/2.7/site-p ...
- gridView在view页面中的一些代码详细模板
<?php use yii\helpers\Html; use yii\grid\GridView; use yii\widgets\Pjax; use frontend\models\Item ...