当前位置:网站首页>力扣练习——15 接雨水
力扣练习——15 接雨水
2022-07-15 22:37:00 【qq_43403657】
15 接雨水
1.问题描述
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。
示例:
输入: [0,1,0,2,1,0,1,3,2,1,2,1]
输出: 6
可使用以下代码,完成其中的trap函数,其中形参height为上述的柱子高度数组,返回接到的雨水量。
#include
#include
#include
using namespace std;
class Solution {
public:
int trap(vector& height)
{
//填充本函数完成功能
}
};
int main()
{
int h;
vector<int> v;
while(cin>>h)
{
v.push_back(h);
}
int res=Solution().trap(v);
cout<<res<<endl;
return 0;
}
2.输入说明
输入若干个非负整数,以空格分隔。
3.输出说明
输出一个整数,表示结果。
4.范例
输入
0 1 0 2 1 0 1 3 2 1 2 1
输出
6
5.代码
#include<iostream>
#include<algorithm>
#include<stack>
#include<vector>
using namespace std;
class Solution {
public:
int trap(vector<int>& height)
//思想:维护一个单调栈,单调栈存储的是下标,满足从栈底到栈顶的下标对应的数组 \textit{height}height 中的元素递减。
//题解https://leetcode.cn/problems/trapping-rain-water/solution/jie-yu-shui-by-leetcode-solution-tuvc/
{
//1.定义初始值
int ans = 0;
stack<int>s;//定义栈
int len = height.size();
//2.遍历并计算接雨量
for (int i = 0; i < len; i++)
{
while (!s.empty() && height[i] > height[s.top()])//计算i处的接雨量
{
int top = s.top();
s.pop();//出栈
if (s.empty())//栈空了,直接退出循环
break;
int left = s.top();//left变成新的top
int currWidth = i - left - 1;//新的宽度
int currHeight = min(height[left], height[i]) - height[top];//新的高度
ans += currWidth * currHeight;
}//while
s.push(i);//在对下标 i 处计算能接的雨水量之后,将 i入栈,继续遍历后面的下标,计算能接的雨水量。
}
return ans;
}
};
int main()
{
int h;
vector<int> v;
while (cin >> h)
{
v.push_back(h);
}
int res = Solution().trap(v);
cout << res << endl;
return 0;
}
边栏推荐
- PendingIntent详解
- LeetCode_513_找树左下角的值
- [OBS] coding CPU performance related problems
- 信息系统项目管理师必背核心考点(四十三)预期货币价值(EMV)
- With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
- Codeforces Round #787 (Div. 3)
- Note: Transformation of gamma distribution
- Can SQL also do AI? you 're right! Mlops meetup V3 review openmlbd+sqlflow+byzer
- 手机号码11位以及格式验证规则
- STM32读取双轴遥感的数据值
猜你喜欢

金融行业开放平台

信息系统项目管理师必背核心考点(四十三)预期货币价值(EMV)

Information system project manager will recite the core examination site (43) expected monetary value (EMV)

Seven uncovered industrial 4.0 misunderstandings

1. Mx6u system migration-3-uboot startup process

IDEA - Could not autowire. No beans of ‘XXXMapper‘ type found.

Maxwell configuration case
[email protected] | Uio-66 / graphene oxide (uio-66 / go) |pt [email protected] |

Stochastic Multiple Choice Learning for Training Diverse Deep Ensembles

Redis - redis list function explanation and industrial application
随机推荐
Uio-66 / graphene oxide (uio-66 / go) |pt [email protected] |
Solve the error of installing GPU version mxnet on Google colab: libnvrtc so. 11.2: cannot open shared object file: No such file...
网络爬虫
Cloud computing in China: prospects for 2025
IDEA - Could not autowire. No beans of ‘XXXMapper‘ type found.
Excerpt of new features in PHP version - php8.0x
PendingIntent详解
Why is everyone keen on taking soft exams? Here's the reason
Deep understanding of modern web browsers (3)
Advanced process: the independence of programmers to do projects
Who else can't use the three methods of de duplication in SQL?
Tab tab switching effect
Record the troubleshooting experience of a pit father memory leak
Reinforcement Learning 强化学习(二)
十分钟快速学习flask框架
封装 avm 组件经验分享
标题太长用省略号代替
Go+mysql+redis+vue3 simple chat room, the second bullet: database links and operations
Li Kou daily question - day 38 -58 Length of the last word
PHP版本新特性摘選 - PHP8.0X