这题放上来是因为自己第一回见到这种题,觉得它好玩儿 =)

Trapping Rain Water

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!

class Solution {
public:
int trap(int A[], int n) {
}
};

题目不难,观察下就可以发现被水填满后的形状是先升后降的塔形,因此,先遍历一遍找到塔顶,然后分别从两边开始,往塔顶所在位置遍历,水位只会增高不会减小,且一直和最近遇到的最大高度持平,这样知道了实时水位,就可以边遍历边计算面积。

从看题目开始一共十分钟,算本菜鸟在AC率二字打头的题目中至今最快的一次。。

class Solution {
public:
int trap(int A[], int n) {
if(n <= ) return ;
int max = -, maxInd = ;
int i = ;
for(; i < n; ++i){
if(A[i] > max){
max = A[i];
maxInd = i;
}
}
int area = , root = A[];
for(i = ; i < maxInd; ++i){
if(root < A[i]) root = A[i];
else area += (root - A[i]);
}
for(i = n-, root = A[n-]; i > maxInd; --i){
if(root < A[i]) root = A[i];
else area += (root - A[i]);
}
return area;
}
};

总结:

这道题和LeetCode上 "Candy" 一题都采用了定义两个指针向中部某一点靠拢的做法(当然Candy还有更快的解,见以前的这篇),这也算是小技巧之一吧,在需要时要能第一时间想到。

[LeetCode] 接雨水,题 Trapping Rain Water的更多相关文章

  1. leetcode 第41题 Trapping Rain Water

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

  2. [Swift]LeetCode407. 接雨水 II | Trapping Rain Water II

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  3. LeetCode(42)Trapping Rain Water

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

  4. LeetCode 42. 接雨水(Trapping Rain Water)

    题目描述 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况 ...

  5. LeetCode 笔记系列12 Trapping Rain Water [复杂的代码是错误的代码]

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

  6. LeetCode: Trapping Rain Water 解题报告

    https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...

  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] 42. Trapping Rain Water 收集雨水

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

  9. [LeetCode] 407. Trapping Rain Water II 收集雨水 II

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

随机推荐

  1. POJ 1655 Balancing Act(求树的重心)

    Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any nod ...

  2. iOS- 网络访问两种常用方式【GET & POST】实现的几个主要步骤

    1.前言 上次,在博客里谈谈了[GET & POST]的区别,这次准备主要是分享一下自己对[GET & POST]的理解和实现的主要步骤. 在这就不多废话了,直接进主题,有什么不足的欢 ...

  3. SQL 单表分页存储过程和单表多字段排序和任意字段分页存储过程

      第一种:单表多字段排序分页存储过程       --支持单表多字段查询,多字段排序 create PROCEDURE [dbo].[UP_GetByPageFiledOrder] ( ), --表 ...

  4. python爬虫 --- 简书评论

    某些网站的一些数据是通过js加载的 ,所以爬取下来的数据拿不到, 找到评论的地址 .进行请求获取评论数据 #coding=utf-8 import json import requests def r ...

  5. C# 开发者最经常犯的 8 个错误

    在和C#新手一起工作的时候,我注意到他们经常重复一些错误.这些错误,当你指出来的时候很容易理解.然而,如果一个开发者没有意识到这些错误,将会影响正在开发的软件的质量和效率,因此,我决定总结8个常见的错 ...

  6. perf中branch-filter到底是干嘛的?

    ./arch/x86/events/intel/core.c:2161:            data.br_stack = &cpuc->lbr_stack;./arch/x86/e ...

  7. Extensions disabled by Chrome

    Extensions disabled by Chrome https://support.google.com/chrome_webstore/answer/2811969 https://supp ...

  8. MyBatis原理简介

    1.什么是 MyBatis ? MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyB ...

  9. Django 2.0 学习(13):Django模板继承和静态文件

    Django模板继承和静态文件 模板继承(extend) Django模板引擎中最强大也是最复杂的部分就是模板继承了,模板继承可以让我们创建一个基本的"骨架"模板,它可以包含网页中 ...

  10. 【题解】CF#852 E-Casinos and travel

    天啊我怎么这么蠢……写了一个树形dp,的确发现记录的很多值并没有什么用,然而当时脑子没转过弯来还是写了这个树形dp……虽然能A但就不解释了,总之是个垃圾算法(ー̀дー́) #include <b ...