/**
* Source : https://oj.leetcode.com/problems/trapping-rain-water/
*
* Created by lverpeng on 2017/7/15.
*
* 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.
*
* ^
* |
* 3 | +--+
* | | |
* 2 | +--+xxxxxxxxx| +--+xx+--+
* | | |xxxxxxxxx| | |xx| |
* 1 | +--+xxx| +--+xxx+--+ | +--+ +--+
* | | |xxx| | |xxx| | | | | | |
* 0 +---+--+---+--+--+---+--+--+--+--+--+--+----->
* 0 1 0 2 1 0 1 3 2 1 2 1
*
* The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case,
* 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
*
*
*
*/
public class TrappingRainWater { /**
* 找到数组组成的柱条之间存水的单位数
*
* 先找到最大的,分为左右两边
* 依次遍历左边,将当前值与左边最大的值比较,如果大于左边最大值则更新左边最大值,计算该位置可以存的水量
* 右边类似
*
*
* @param num
*/
public int trap (int[] num) {
int max = 0;
int maxIndex = 0;
for (int i = 0; i < num.length; i++) {
if (max < num[i]) {
max = num[i];
maxIndex = i;
}
} // 当前位置以前的较大值
int preMax = 0;
int result = 0;
// 计算左边
for (int i = 0; i < maxIndex; i++) {
if (preMax < num[i]) {
preMax = num[i];
}
result += preMax - num[i];
} // 计算右边
int afterMax = 0;
for (int i = num.length - 1; i > maxIndex ; i--) {
if (afterMax < num[i]) {
afterMax = num[i];
}
result += afterMax - num[i];
} return result;
} public static void main(String[] args) {
TrappingRainWater trappingRainWater = new TrappingRainWater();
int[] arr = new int[]{0,1,0,2,1,0,1,3,2,1,2,1};
System.out.println(trappingRainWater.trap(arr));
}
}

leetcode — trapping-rain-water的更多相关文章

  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 Trapping Rain Water pthon

    class Solution(object): def trap(self,nums): leftmosthigh = [0 for i in range(len(nums))] leftmax=0 ...

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

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

随机推荐

  1. mybatisGenerator代码生成器

    使用mybatisGenerator可以生成实体类,Mapper接口以及对应xml文件.本文介绍如何使用. 可以直接从本人github下载,只需按照如下步骤即可: 1.导入项目至idea中,项目结构如 ...

  2. Java Web程序开发链接MySQL数据库

    显示错误:Access denied for user ''@'localhost' (using password: YES) 保证URL中没有空格 尝试用MySQL本地命令行登陆 显示错误:The ...

  3. 【repost】图解Javascript上下文与作用域

    本文尝试阐述Javascript中的上下文与作用域背后的机制,主要涉及到执行上下文(execution context).作用域链(scope chain).闭包(closure).this等概念. ...

  4. winform窗体退出

    winform主窗体退出需要在FormClosing事件里写入程序退出代码,防止程序明明退出了,而程序进程还没杀掉: private void FormMain_FormClosing(object ...

  5. Delphi ClientDataSet复制记录

    数据源记录集:ClientDataSetSource:目标记录集:ClientDataSetCopy 1)复制一条记录. ClientDataSetCopy.Close;  ClientDataSet ...

  6. 升讯威微信营销系统开发实践:所见即所得的微官网( 完整开源于 Github)

    GitHub:https://github.com/iccb1013/Sheng.WeixinConstruction因为个人精力时间有限,不会再对现有代码进行更新维护,不过微信接口比较稳定,经测试至 ...

  7. 【高速接口-RapidIO】5、Xilinx RapidIO核例子工程源码分析

    提示:本文的所有图片如果不清晰,请在浏览器的新建标签中打开或保存到本地打开 一.软件平台与硬件平台 软件平台: 操作系统:Windows 8.1 64-bit 开发套件:Vivado2015.4.2 ...

  8. springDatasolr 排序

    String sortValue = (String) searchMap.get("sort");// ASC DESC String sortField = (String) ...

  9. MySQL注入技巧性研究

    0x00 前言 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一,本人最近针对MySQL注入做了 ...

  10. SpringMVC 全局异常处理

    在 JavaEE 项目的开发中,不管是对底层的数据库操作过程,还是业务层的处理过程,还是控制层的处理过程,都不可避免会遇到各种可预知的.不可预知的异常需要处理.每个过程都单独处理异常,系统的代码耦合度 ...