/**
* 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. 20175316盛茂淞 2018-2019-2 《Java程序设计》第7周学习总结

    20175316盛茂淞 2018-2019-2 <Java程序设计>第7周学习总结 教材学习内容总结 第八章 常用实用类 一.String类 String类在java.lang包中,jav ...

  2. k8s storageClass对接nfs

    前提:已存在一个nfs服务 192.168.137.11:/home/nfs_data 下面以rbac方式对接nfs 1.创建/root/k8s-nfs-rbac/serviceaccount.yam ...

  3. Linux下安装numpy

    转自:https://blog.csdn.net/abc_321a/article/details/82056019 1.下载源码包 ,命令如下 wget http://jaist.dl.source ...

  4. JSON.parse()——Uncaught SyntaxError: Unexpected token \ in JSON at position 1

    背景:项目安全处理方面之一 ——对特殊字符进行编解码(后端编码,前端解码) 特殊字符: "    %22 \    %5C /    %2F &   %26 %   %25 '    ...

  5. windows和ubuntu下git commit提交后如何保存和退出,回到命令行

    问题一: windows下git commit后会进入vim界面,不知道怎么操作 解决办法: 1.输入小写字母i,此时进入编辑模式,可以输入你想输入的内容 2.按下esc键,此时退出编辑模式,输入英文 ...

  6. let,const,var

    1.const定义的变量不可以修改,而且必须初始化. 1 const b = 2;//正确 2 // const b;//错误,必须初始化 3 console.log('函数外const定义b:' + ...

  7. 26.HashCode

      在前面三篇博文中讲解了(HashMap.HashSet.HashTable),在其中不断地讲解他们的put和get方法,在这两个方法中计算key的hashCode应该是最重要也是最精华的部分,所以 ...

  8. 大数据开发主战场hive (企业hive应用)

    hive在大数据套件中占很的地位,分享下个人经验. 1.在hive日常开发中,我们首先面对的就是hive的表和库,因此我要先了解库,表的命名规范和原则 如 dwd_whct_xmxx_m 第1部分为表 ...

  9. NodeJs在windows上安装配置测试

    Node.js简介简单的说 Node.js 就是运行在服务端的 JavaScript.Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境.Node.js 使用了一个 ...

  10. 【腾讯Bugly干货分享】职场中脱颖而出的成长秘诀

    本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:http://mp.weixin.qq.com/s/uQKpVg7HMLfogGzzMyc9iQ 导语 时光 ...