一天一道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. PHP 实例 AJAX 与 XML

    在 PHP 中,AJAX 可用来与 XML 文件进行交互式通信,具体的通信过程,请参考本文内容! AJAX XML 实例 下面的实例将演示网页如何通过 AJAX 从 XML 文件读取信息: 实例   ...

  2. CentOS7: How to resolve curl#56 - "Recv failure: Connection reset by peer"

    Issue: When you execute Yum installation or update, you may encounter following error: Loaded plugin ...

  3. 20160217.CCPP体系详解(0027天)

    程序片段(01):TestCmd.c 内容概要:管道_字符串 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include < ...

  4. sklearn:聚类clustering

    http://blog.csdn.net/pipisorry/article/details/53185758 不同聚类效果比较 sklearn不同聚类示例比较 A comparison of the ...

  5. 2017京东校招面试回忆(已成功拿到offer)

    一面 24日 晚上5:30-6:40 1 先说自己熟悉的领域 2 list的实现有什么?   arraylist1.6 1.7区别  底层   linkedlist 底层是怎么实现的 单向还是双向   ...

  6. protobuf中的枚举缺省值应该为UNKNOWN

    protobuf中的枚举缺省值应该为UNKNOWN(金庆的专栏)proto3中的枚举值为了与proto2兼容,要求缺省值固定为第1个,值为0.proto2中并没有规定对范围之外的枚举值的处理,而pro ...

  7. Web自动化框架LazyUI使用手册(8)--excel数据驱动详解(ExcelDataProvider)

    概述 框架提供了excel数据驱动方式运行测试用例的工具,本文将针对数据驱动,进行详细演示. 详见类:lazy.test.ui.browser.ExcelDataProvider 被测对象: http ...

  8. linux简单命常用令

    Linux常用命令总结 切换:cd tmp cd/tmp/yun cd 切换到host目录 cd .. 显示:ll Top显示系统情况 Netstat显示网络情况 Ifconfig显示网络配置 Mor ...

  9. Building System之 get_abs_build_var() && get_build_var()

    点击打开链接 1.get_abs_build_var() 和 get_build_var()的实现都在build/envsetup.sh中. 2.在buld目录下grep这两个函数可知:这两个函数只在 ...

  10. 【java虚拟机系列】java虚拟机系列之JVM总述

    我们知道java之所以能够快速崛起一个重要的原因就是其跨平台性,而跨平台就是通过java虚拟机来完成的,java虚拟机属于java底层的知识范畴,即使你不了解也不会影响绝大部分人从事的java应用层的 ...