一天一道LeetCode系列

(一)题目

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.

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!

(二)解题

题目需要求的是矩阵作为容器能盛多少体积的水。

方法可以参考我的这篇博客:【一天一道LeetCode】#11Container With Most Water

/*
思路:考虑到所盛的水取决于容器两端中小的那一端。因此用两个指针,分别指向头和尾,依次向中间移动。
1.如果左边小,则左边向右移动一格,这个时候需要判断向右移动一格后
①如果高度大于原来的就表示盛不下水
②如果小于原来的则表示有凹下去的部分,这个时候计算高度差就代表能盛多少水。(右边比左边高,可以保证右边不溢出)
2.如果右边小,则右边向左移动一格,这个时候同1一样判断。
*/
class Solution {
public:
    int trap(vector<int>& height) {
        if(height.size()<=2) return 0;
        int ret = 0;
        int l = 0;
        int r = height.size()-1;
        int left = height[0];
        int right = height[r];
        while(l<r)
        {
            if(left<=right)
            {
                l++;
                if(height[l]>=left)
                {
                    left = height[l];
                }
                else ret+=(left-height[l]);
            }
            else
            {
                r--;
                if(height[r]>=right)
                {
                    right = height[r];
                }
                else ret +=(right-height[r]);
            }
        }
        return ret;
    }
};

【一天一道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

    42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...

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

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

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

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

  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雨水积水问题

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

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

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

  10. Java [Leetcode 42]Trapping Rain Water

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

随机推荐

  1. 通讯协议序列化解读(一) Protobuf详解教程

    前言:说到JSON可能大家很熟悉,是目前应用最广泛的一种序列化格式,它使用起来简单方便,而且拥有超高的可读性.但是在越来越多的应用场景里,JSON冗长的缺点导致它并不是一种最优的选择. 一.常用序列化 ...

  2. JavaScript 注释

    JavaScript 注释可用于提高代码的可读性. JavaScript 注释 JavaScript 不会执行注释. 我们可以添加注释来对 JavaScript 进行解释,或者提高代码的可读性. 单行 ...

  3. Nginx之(四)工作原理

    众所周知,nginx性能高,而nginx的高性能与其架构是分不开的 4.1 进程模型 Nginx在启动后,会有一个master进程和多个worker进程.master进程主要用来管理worker进程, ...

  4. 安卓高级9 shareSDK 第三方登陆和分享Mob

    ShareSDK推荐此官网地址 官网注册和申请key流程 注册 略 进入后台创建应用得到可以 下载SDK 解压下载的压缩包 第三方平台申请key 如果你想申请QQ登陆或者分享需要去腾讯开发者申请,申请 ...

  5. Redis之(四)事务

    5.1开始事务 MULTI 命令的执行标记着事务的开始: 当客户端处于非事务状态下时, 所有发送给服务器端的命令都会立即被服务器执行. Redis 的事务不可嵌套, 当客户端已经处于事务状态, 而客户 ...

  6. ERROR: Cannot change version of project facet Dynamic Web Module to 3.0?

    Issue: When you create web app in eclipse with maven configuration, you may get following error. Can ...

  7. 用类模拟C风格的赋值+返回值

    这个方法比较好: class DataHolder: def __init__(self, value=None): self.value = value def set(self, value): ...

  8. [apache2.4]configure: error: APR not found. Please read the documentation.

    apache2.4 安装出现如下错误 ``` [lzz@localhost httpd-2.4.10]$ ./configure  checking for chosen layout... Apac ...

  9. 使用 纯JQuery 进行 表单 验证

    对于JavaScript而言,进行表单数据的验证可谓是很有必要的,而且一般我们都会在网页上先进行一下表单验证,然后服务器端再次进行验证,来确保用户提交数据的准确性.下面就来分享一个JQuery实现的表 ...

  10. Gazebo機器人仿真學習探索筆記(四)模型編輯

    模型編輯主要是自定義編輯物體模型構建環境,也可以將多種模型組合爲新模型等,支持外部模型導入, 需要注意的導入模型格式有相應要求,否在無法導入成功, COLLADA (dae), STereoLitho ...