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!

Hide Tags

Array Stack Two Pointers

 

     好开心,逻辑这么麻烦的题目,写了一次没有错,提交了直接过。
     做了前面题目可能容易想,假如用一个stack 维护下标,其对应的数列 是非升序的,后面的工作中就维护这一个stack。在一个非降序的stack,如果考虑的值比top 小,则加入stack。如果大于top 值,则弹出小于的值,计算return 值。
     例如遍历到idx =5时候,stack 里面的 下标有:  3,4
  a[5]<a[4],所以加入stack,然后遍历到idx =6时候,stack为:3,4,5
  那么弹出 stack 的top,然后计算弹出值水坑,这时候只需要计算红色的部分,通过stack 弹出后的top=4,坑=5,和墙=6,的时候更新,
  然后更新stack 变成:3,4,6,接着便是遍历idx =7,这时候弹出的是6,通过top=4,坑6,墙=7 计算,这个值为0.
  因为stack 的top 还是小于,所以继续弹出,然后计算,top=3,坑4,墙7,计算了下图的部分:
 
 
 此时stack: 3,继续弹出,但此时stack 没有top 了,所以可以结束这一步,将idx=7 压入stack。
 
代码:
 
 #include <iostream>
#include <stack>
using namespace std; class Solution {
public:
int trap(int A[], int n) {
if(n<) return ;
int curIdx = ; stack<int> stk; int retSum = ;
for(;curIdx<n;curIdx++){
if(stk.empty()){
stk.push(curIdx);
continue;
}
int stkTop = stk.top();
if(A[stkTop]>=A[curIdx]){
stk.push(curIdx);
continue;
}
while(!stk.empty()){
int dit = stkTop;
stk.pop();
if(stk.empty()) break;
stkTop =stk.top();
retSum += (min(A[stkTop],A[curIdx])-A[dit])*(curIdx-stkTop - );
if(A[stkTop]>A[curIdx]) break;
}
stk.push(curIdx);
}
return retSum;
}
}; int main()
{
int A[]= {,,,,,,,,,,,};
Solution sol;
cout<<sol.trap(A,sizeof(A)/sizeof(int))<<endl;
return ;
}
 
 
 
 

[LeetCode] Trapping Rain Water 栈的更多相关文章

  1. [LeetCode] Trapping Rain Water 收集雨水

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

  2. [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 ...

  3. LeetCode: Trapping Rain Water 解题报告

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

  4. 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 ...

  5. [leetcode]Trapping Rain Water @ Python

    原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: Given n non-negative integers represe ...

  6. Leetcode 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 题解

    题意 题目 思路 我一开始想的时候只考虑到一个结点周围的边界的情况,并没有考虑到边界的高度其实影响到所有的结点盛水的高度. 我们可以发现,中间是否能够盛水取决于边界是否足够高于里面的高度,所以这必然是 ...

  8. leetcode Trapping Rain Water pthon

    class Solution(object): def trap(self,nums): leftmosthigh = [0 for i in range(len(nums))] leftmax=0 ...

  9. leetcode#42 Trapping rain water的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...

随机推荐

  1. Mysql关闭和修改密码

    数据库的关闭方法: 1.优雅的关闭数据库的方法:mysqladmin -uroot -p123456 shutdown 2.脚本关闭:/etc/init.d/mysqld stop 3.使用kill信 ...

  2. Python循环的一些基本练习

    #1:# name = input('请输入你的身份')# if name == 'egon':# print('--> 超级管理员')# elif name == 'tom':# print( ...

  3. 50 道 CSS 基础面试题及答案

    1 介绍一下标准的CSS的盒子模型?与低版本IE的盒子模型有什么不同的? 标准盒子模型:宽度=内容的宽度(content)+ border + padding + margin 低版本IE盒子模型:宽 ...

  4. python之格式化

    python有两种方式可以格式化一种是用%s,一种使用format(2.6)进入的,从下面的代码可以看出,效果差不多. name = 'edward' age = 27 print("My ...

  5. Unity 与Mono和.Net的关系

    一.分析 首先,我们要知道Unity,Mono,.Net 三者的关系.需要简单说一下.Net. .Net拥有跨语言,跨平台性. 跨语言:就是只要是面向.Net平台的编程语言,用其中一种语言编写的类型就 ...

  6. 第三模块 面向对象& 网络编程基础 实战考核

    1.简述构造方法和析构方法. 构造方法(__init__):主要作用是实例化时给实例一些初始化参数,或执行一些其它的初始化工作,总之因为这个__init__只要一实例化, 就会自动执行,不管你在这个方 ...

  7. Excel动画教程50例(三)

    Excel动画教程50例(三) 31.Excel自定输入数据下拉列表 32.Excel正确输入身份证号码 33.Excel数据排序操作 34.Excel数据表格中如何将姓名信息按笔画排列 35.Exc ...

  8. 使用Jmeter做性能测试

    上周刚刚做完项目的性能测试.今天整理和总结一下,随便分享给大家. 首页呢,测试前,我们是有明确的性能指标的,而且测试环境和数据都已准备好,业务分析.场景分析大家根据自己的项目系统进行分析设计,我们选用 ...

  9. Python-S9-Day114——Flask开始实战

    01 今日内容概要 02 课前分享 03 内容回顾 04 路飞学城表结构(一) 05 路飞学城表结构(二) 06 路飞学城立即支付思路 07 今日作业 08 初识Flask 09 werkzug 10 ...

  10. table中填写数据并批量增加

    <table class = "table jtable table-bordered table-striped hide" id = "table_1" ...