algorithm-base/animation-simulation/单调队列单调栈/接雨水.md

134 lines
7.5 KiB
Java
Raw Normal View History

2021-03-20 11:38:55 +00:00
> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
>
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
2021-03-20 05:38:47 +00:00
2021-03-20 11:38:55 +00:00
#### [42. ](https://leetcode-cn.com/problems/trapping-rain-water/)
2021-03-19 08:36:59 +00:00
n 1
1
```
height = [0,1,0,2,1,0,1,3,2,1,2,1]
6
```
2
```
height = [4,2,0,3,2,5]
9
```
3
```
[4,3,2,0,1,1,5]
13
```
> [4,3,2,0,1,1,5] 13
###
3
2021-03-20 05:43:55 +00:00
![](https://img-blog.csdnimg.cn/2021032013412768.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzODg1OTI0,size_16,color_FFFFFF,t_70)
2021-03-19 08:36:59 +00:00
4234
2021-03-20 05:45:03 +00:00
<img src="https://img-blog.csdnimg.cn/20210320134154434.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzODg1OTI0,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述" style="zoom:80%;" />
2021-03-19 08:36:59 +00:00
3
43201001
2021-03-20 05:43:55 +00:00
![](https://img-blog.csdnimg.cn/20210320134213324.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzODg1OTI0,size_16,color_FFFFFF,t_70)
2021-03-19 08:36:59 +00:00
4320102201()
2021-03-20 05:43:55 +00:00
![](https://img-blog.csdnimg.cn/20210320134228696.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzODg1OTI0,size_16,color_FFFFFF,t_70)
2021-03-19 08:36:59 +00:00
0143211432115112
2021-03-20 05:43:55 +00:00
![](https://img-blog.csdnimg.cn/20210320134249605.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzODg1OTI0,size_16,color_FFFFFF,t_70)
2021-03-19 08:36:59 +00:00
21151*3=3;1min(2-1,5-1)=min(1,4)13562234536-2-1=3
123325
2021-03-20 05:43:55 +00:00
![](https://img-blog.csdnimg.cn/20210320134307389.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzODg1OTI0,size_16,color_FFFFFF,t_70)
2021-03-19 08:36:59 +00:00
42435
2021-03-20 05:43:55 +00:00
![](https://img-blog.csdnimg.cn/20210320134319646.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzODg1OTI0,size_16,color_FFFFFF,t_70)
2021-03-19 08:36:59 +00:00
5
1+3+4+5=13
![](https://img-blog.csdnimg.cn/20210319163622150.gif)
###
```java
class Solution {
public int trap(int[] height) {
Stack<Integer> stack = new Stack<Integer>();
int water = 0;
//特殊情况
if(height.length <3){
return 0;
}
for(int i = 0; i < height.length; i++){
while(!stack.isEmpty() && height[i] > height[stack.peek()]){
//栈顶元素
int popnum = stack.pop();
//相同元素的情况例11
while(!stack.isEmpty()&&height[popnum] == height[stack.peek()]){
stack.pop();
}
//计算该层的水的单位
if(!stack.isEmpty()){
int temp = height[stack.peek()];//栈顶元素值
//高
int hig = Math.min(temp-height[popnum],height[i]-height[popnum]);
//宽
int wid = i-stack.peek()-1;
water +=hig * wid;
}
}
//这里入栈的是索引
stack.push(i);
}
return water;
}
}
```
2021-03-20 05:38:47 +00:00
###