题目:

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!

题目意思应该很好理解,就是给定数组后,如图,能存放多少单位的水。网上大多数人的方法都是累加每柱能容纳的水。例如。我的做法是,累加当前到之后第一个不小于自己的柱子能存的水。定义一个start,每次计算完一块水域之后,start跳到这块水域的右边柱子往后找新的水域。直到最后。应该是O(n)的。现在就出现问题了,如果出现4 2 3 的例子,如果按照刚才的思路,4的时候,后面没有大于等于4的数,那start就++的话,答案最终是零了。解决的办法是,如果之后没有发现比大于等于自己的柱子,那么就将自己减一,再判断,知道自己为零,或者是找到不小于自己的柱子为止。

oj上证明我的方法比别人快5倍。

class Solution {
public:
int trap(int A[], int n)
{
int sum = 0, start = -1;
while(++start < n && A[start] == 0);// 找到第一个非零为start
while(start < n)
{
int next = start, minus = 0;
while(++next < n && A[next] < A[start])
{
minus += A[next];
}
if (next == n) // 说明之后没有比当前大或者相等的数,所以要将A[start]值减一后再判断,直到有找到不小于当前的数或者已经减到0了
{
A[start] -= 1;
if (A[start] > 0)//只要大于零就要再判断之后有没有不小于当前的数
continue;
}
sum += (next - start - 1) * A[start] - minus;
start = next;
}
return sum;
}
};

发现oj上如果把注释去掉,居然更快。。。

2015/04/03: 只扫描一遍,left从左往右,right从右往左,每次比较小的往里走,走到比自己大的位置,过程当中记录可以盛的水。(阿里2015校招实习生笔试题)

class Solution {
public:
int trap(int A[], int n) {
int left = , right = n - , water = ;
while(left < right){
if (A[left] < A[right]){
int i = left + ;
while(i < right && A[i] < A[left]){
water += A[left] - A[i++];
}
left = i;
}
else{
int i = right - ;
while(i > left && A[i] < A[right]){
water += A[right] - A[i--];
}
right = i;
}
}
return water;
}
};

扫描两遍:第一遍从左往右,记录当前左边的最大, 第一遍从右往左,记录到当前的最大,根据左边和右边以及自己的数,就可以判断当前是否可以盛水。

class Solution {
public:
int trap(int A[], int n) {
if (n <= ) return ;
int water = , perm[n], tmpMax = ;
for (int i = ; i < n; ++i){
if (tmpMax < A[i-]){
tmpMax = A[i-];
}
perm[i] = tmpMax;
}
tmpMax = ;
for (int i = n - ; i > ; --i){
if (tmpMax < A[i+]){
tmpMax = A[i+];
}
water += min(perm[i], tmpMax) - A[i] > ? min(perm[i], tmpMax) - A[i] : ;
}
return water;
}
};

leetcode 第41题 Trapping Rain Water的更多相关文章

  1. [LeetCode] 接雨水,题 Trapping Rain Water

    这题放上来是因为自己第一回见到这种题,觉得它好玩儿 =) Trapping Rain Water Given n non-negative integers representing an eleva ...

  2. LeetCode(42)Trapping Rain Water

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

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

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

  4. LeetCode: Trapping Rain Water 解题报告

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

  5. [LeetCode] 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 收集雨水

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

  7. [LeetCode] Trapping Rain Water II 收集雨水之二

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

  8. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

  9. [Leetcode][Python]42: Trapping Rain Water

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...

随机推荐

  1. Android checkbox和radiobutton 以及Toast和AlertDialog的使用

    package com.example.radiobutton_01; import android.app.Activity; import android.os.Bundle; import an ...

  2. oracle_控制用户权限

     oracle_控制用户权限 ①对 限制     数据库安全:     系统安全     数据安全性     系统权限: 对于数据库的权限     对象权限: 操作数据库对象的权限 1.系统权限 ...

  3. MapXtreme DJ最短路径算法 全路径搜索算法

    包括最短路径,全路径搜索算法演示程序请在http://pan.baidu.com/s/1jG9gKMM#dir/path=%2F%E4%BA%A7%E5%93%81%2FDemos 找 ShortWa ...

  4. mount命令使用具体解释(Linux)

    linux是一个优秀的开放源代码的操作系统,能够执行在大到巨型小到掌上型各类计算机系统上,随着 linux系统的日渐成熟和稳定以及它开放源代码特有的优越性,linux在全世界得到了越来越广泛的应用. ...

  5. Javascript入门视频教程

    1,第一节 http://pan.baidu.com/play/video#video/path=%2F%E6%95%99%E5%AD%A61.mov&t=-1 2,第二节 http://pa ...

  6. iOS一个开发系列中 - UIButton 使用摘要

    // 初始化button并设置类型 UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // 可以定义的UIButto ...

  7. UVa 699 落叶

    意甲冠军:我几乎不记得的题意!.! 它是一个长坑..我们从根节点开始,留下每一步,保存横坐标1,正确的一步加上横坐标1. 那么同样的横坐标统计data值总和. 思维:我来想,这可以递归成就.上就能够算 ...

  8. iptables惹的祸

    我的工作环境总是一台Mac机+在Mac机上跑的虚拟机(Virtual Box) 有一个错误我总是在犯:宿主机总是无訪问到虚拟机里的web服务. 解决方法也非常easy:那就是关闭虚拟机上的防火墙!!! ...

  9. 【 Android官方文件读书笔记】连接网络

    一间连接应用网络的主要功能.Android系统对网络连接进行了封装,使得开发人员可以更快的给应用添加网络功能.大多数网络连接的Android应用使用HTTP发送和接受数据.Android包含两个HTT ...

  10. MvcOptions配置

    MvcOptions配置 程序模型处理 IApplicationModelConvention 在MvcOptions的实例对象上,有一个ApplicationModelConventions属性(类 ...