一天一道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. Canvas实现3D效果-可旋转的立方体

    摘要:Canvas画布是一个二维平面,如何展示出3D效果?通过将三维空间中的Z轴抽取出来,将图像的点投影到与Z轴垂直的平面上,在通过旋转等变换效果,我们就能实现3D效果. 一.建立坐标系 1)立方体坐 ...

  2. Java日志-Log4j2

    Log4j2参考资料 Log4j2 官方配置文档 1. Log4j2基本概念: Logger 在代码中产生日志信息的.比如logger.info("some log message" ...

  3. POSIX 消息队列相关问题

    一.查看和删除消息队列要想看到创建的posix消息队列,需要在root用户下执行以下操作:# mkdir /dev/mqueue# mount -t mqueue none /dev/mqueue删除 ...

  4. pg备份恢复与设置编码

    psql create database jasygl encoding='UTF8' TEMPLATE = template0; 命令行 备份 pg_dump dabase_name > ba ...

  5. ToolBar控件详解

    ToolBar控件详解 在Activity中添加ToolBar 1.添加库 dependencies { ... compile "com.android.support:appcompat ...

  6. java获取ip的方式,注意多级代理的方式获取

    public String getIP() { String clientIP = ServletActionContext.getRequest().getHeader("x-forwar ...

  7. 针对于Python的OpenCV环境搭建

    OpenCV 依赖 下载OpenCV 配置 总结 给Python搭建opencv的环境还真是略嫌麻烦,于是做下笔记,以备不时之需. OpenCV 依赖 opencv有些依赖,我们必须安装一下,否则接下 ...

  8. FFmpeg与libx264接口源代码简单分析

    ===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...

  9. Linux的sleep()和usleep()

    1.sleep和usleep都是linux中的程序挂起函数.只是时间的单位不一样. 2. sleep的基本单位是s(秒),也可以用m(分).h(小时). 例: sleep 1 : 挂起1秒 sleep ...

  10. JRE System Library [JavaSE-1.7](unbound)

    window > preferences > java > Install jars >如果没有jdk1.7 ,点击下面的search,会自动找到已经安装对jdk1.7,选择, ...