42. Trapping Rain Water

Problem's Link

----------------------------------------------------------------------------

Mean:

在坐标上给你一些竖直放置的条形积木,问你这个积木能够容纳多少液体.

analyse:

首先找出最高的积木,然后从前往后一直扫到最高积木,从后往前一直扫到最高积木,两部分体积相加即可.

Time complexity: O(N)

view code

;
       ;
       ;;
           ;
           );
       ;
           ;
           );;
}
/*

*/

2.方法二:时间O(n),空间(1)

;
       ;
       ;
       while(left<right)
       {
           if(height[left]<=height[right])
           {
               if(height[left]>maxLeft)
                   maxLeft=height[left];
               res+=maxLeft-height[left];
               left++;
           }
           else
           {
               if(height[right]>maxRight)
                   maxRight=height[right];
               res+=maxRight-height[right];
               right--;
           }
       }
       return res;
   }
};

LeetCode - 42. Trapping Rain Water的更多相关文章

  1. leetcode#42 Trapping rain water的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...

  2. [array] leetcode - 42. Trapping Rain Water - Hard

    leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...

  3. LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))

    LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...

  4. [LeetCode] 42. Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  5. leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

  6. [LeetCode] 42. Trapping Rain Water 解题思路

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  7. [leetcode]42. Trapping Rain Water雨水积水问题

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  8. LeetCode 42 Trapping Rain Water(积水体积)

    题目链接: https://leetcode.com/problems/trapping-rain-water/?tab=Description   Problem: 根据所给数组的值,按照上图的示意 ...

  9. Java [Leetcode 42]Trapping Rain Water

    题目描述: Given n non-negative integers representing an elevation map where the width of each bar is 1, ...

随机推荐

  1. 机器学习基石--学习笔记01--linear hard SVM

    背景 支持向量机(SVM)背后的数学知识比较复杂,之前尝试过在网上搜索一些资料自学,但是效果不佳.所以,在我的数据挖掘工具箱中,一直不会使用SVM这个利器.最近,台大林轩田老师在Coursera上的机 ...

  2. JdbcTemplate queryForMap EmptyResultDataAccessException

    JdbcTemplate的queryForMap方法报错 queryForMap方法使用不当,就会出错,使用方式如下: The queryForMap method in JdbcTemplate o ...

  3. http协议读书笔记1-概述

    1.http协议在网络中的位置: http协议位于TCP协议的上层,http试用tcp来传输其报文数据,tcp在ip的上层. 2.浏览器发起连接的过程 上述图的过程是: 浏览器从url中解析出服务区的 ...

  4. 【转】开启Apache mod_rewrite模块完全解答

    启用mod_rewrite模块在conf目录的httpd.conf文件中找到LoadModule rewrite_module modules/mod_rewrite.so将这一行前面的#去掉.2.在 ...

  5. Django 源码小剖: 应用程序入口 WSGIHandler

    WSGI 有三个部分, 分别为服务器(server), 应用程序(application) 和中间件(middleware). 已经知道, 服务器方面会调用应用程序来处理请求, 在应用程序中有真正的处 ...

  6. Solr4:数据导入(dataimport)时,不符合Solr日期类型要求的字段的处理

    背景: 要求将一个SQL Server2012版本中的数据库导入到Solr中.数据表中有一字段用来存储birthday日期字段,为nvarchar类型,长度为8,格式为:yyyyMMdd. 导入Sol ...

  7. Maven3路程(六)用Maven创建Spring3 MVC项目

    Maven3路程(六)用Maven创建Spring3 MVC项目 一.      环境 spring-framework-3.2.4.RELEASE jdk1.7.0_11 Maven3.0.5 ec ...

  8. java之接口interface

    接口 1.多个无关的类可以实现同一个接口 2.一个类可以实现多个无关的接口 3.与继承关系类似,接口与实现类之间存在多态性 4.定义java类的语法格式 < modifier> class ...

  9. POJ 2234 Matches Game

    Matches Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7567   Accepted: 4327 Desc ...

  10. Redis内存数据库在Exchange会议室的应用

    本文论述了现有Exchange会议室应用现状和不足之处,并详细介绍了Redis内存数据库在Exchange会议室的应用,并给出了一种高性能的应用架构及采用关键技术和关键实现过程,最终实现大幅改进系统性 ...