[LeetCode] 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!
好开心,逻辑这么麻烦的题目,写了一次没有错,提交了直接过。
#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 栈的更多相关文章
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [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 ...
- LeetCode: Trapping Rain Water 解题报告
https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...
- 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 ...
- [leetcode]Trapping Rain Water @ Python
原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: Given n non-negative integers represe ...
- Leetcode Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] Trapping Rain Water II 题解
题意 题目 思路 我一开始想的时候只考虑到一个结点周围的边界的情况,并没有考虑到边界的高度其实影响到所有的结点盛水的高度. 我们可以发现,中间是否能够盛水取决于边界是否足够高于里面的高度,所以这必然是 ...
- leetcode Trapping Rain Water pthon
class Solution(object): def trap(self,nums): leftmosthigh = [0 for i in range(len(nums))] leftmax=0 ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
随机推荐
- Mysql关闭和修改密码
数据库的关闭方法: 1.优雅的关闭数据库的方法:mysqladmin -uroot -p123456 shutdown 2.脚本关闭:/etc/init.d/mysqld stop 3.使用kill信 ...
- Python循环的一些基本练习
#1:# name = input('请输入你的身份')# if name == 'egon':# print('--> 超级管理员')# elif name == 'tom':# print( ...
- 50 道 CSS 基础面试题及答案
1 介绍一下标准的CSS的盒子模型?与低版本IE的盒子模型有什么不同的? 标准盒子模型:宽度=内容的宽度(content)+ border + padding + margin 低版本IE盒子模型:宽 ...
- python之格式化
python有两种方式可以格式化一种是用%s,一种使用format(2.6)进入的,从下面的代码可以看出,效果差不多. name = 'edward' age = 27 print("My ...
- Unity 与Mono和.Net的关系
一.分析 首先,我们要知道Unity,Mono,.Net 三者的关系.需要简单说一下.Net. .Net拥有跨语言,跨平台性. 跨语言:就是只要是面向.Net平台的编程语言,用其中一种语言编写的类型就 ...
- 第三模块 面向对象& 网络编程基础 实战考核
1.简述构造方法和析构方法. 构造方法(__init__):主要作用是实例化时给实例一些初始化参数,或执行一些其它的初始化工作,总之因为这个__init__只要一实例化, 就会自动执行,不管你在这个方 ...
- Excel动画教程50例(三)
Excel动画教程50例(三) 31.Excel自定输入数据下拉列表 32.Excel正确输入身份证号码 33.Excel数据排序操作 34.Excel数据表格中如何将姓名信息按笔画排列 35.Exc ...
- 使用Jmeter做性能测试
上周刚刚做完项目的性能测试.今天整理和总结一下,随便分享给大家. 首页呢,测试前,我们是有明确的性能指标的,而且测试环境和数据都已准备好,业务分析.场景分析大家根据自己的项目系统进行分析设计,我们选用 ...
- Python-S9-Day114——Flask开始实战
01 今日内容概要 02 课前分享 03 内容回顾 04 路飞学城表结构(一) 05 路飞学城表结构(二) 06 路飞学城立即支付思路 07 今日作业 08 初识Flask 09 werkzug 10 ...
- table中填写数据并批量增加
<table class = "table jtable table-bordered table-striped hide" id = "table_1" ...