当前位置:网站首页>Leetcode 739.每日温度 单调栈
Leetcode 739.每日温度 单调栈
2022-08-09 18:05:00 【Alkali!】
问题描述
思路
非常直接的单调栈问题
是这一道题的变种
leetcode 503.下一个更大元素II
代码
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
pair<int,int> tem[temperatures.size()];
for(int i=0;i<temperatures.size();i++)
tem[i]={
i,temperatures[i]};
stack<pair<int,int>> s; //栈内存储的元素得既有下标又有温度,因为这里求的是相隔的天数
vector<int> res;
for(int i=temperatures.size()-1;i>=0;i--)
{
while(!s.empty()&&s.top().second<=tem[i].second) //比较的是温度
s.pop();
if(s.empty())
res.push_back(0);
else
res.push_back(s.top().first-tem[i].first); //实际输出的是天数
s.push(tem[i]);
}
reverse(res.begin(),res.end());
return res;
}
};
边栏推荐
猜你喜欢
混动产品助力,自主SUV市场格局迎来新篇章
jmeter - record script
没有 accept,TCP 连接可以建立成功吗?
JSDN blog system
牛客网 Verilog 在线编程题库解答(VL1~VL10)
什么是藏宝计划(TPC),2022的一匹插着翅膀的黑马!
毕昇编译器优化:Lazy Code Motion
Intensive reading of the paper: VIT - AN IMAGE IS WORTH 16X16 WORDS: TRANSFORMERS FOR IMAGE RECOGNITION AT SCALE
Samsung's flagship discount is 1,800, Apple's discount is over 1,000, and the domestic flagship is only reduced by 500 to send beggars
字节二面:可重复读隔离级别下,这个场景会发生什么?
随机推荐
英赛克工控安全项目入围《钢铁行业智能制造解决方案推荐目录》
web正则表达式中^和$的含义是什么
[Free Column] Android Fragment Injection for Android Security
How to stop the test after reaching a given number of errors during stress testing in JMeter
ARM 汇编基础
什么是ROS
混动产品助力,自主SUV市场格局迎来新篇章
毕昇编译器优化:Lazy Code Motion
MySQL备份与恢复
网络安全:常见的网络协议
ceph集群部署
说了半天跨平台,今儿咱就来跨跨!(完结篇)——Kubenetes上手实践
与同步传递相关的获取-释放序列
Simple prohibition of garbage collection in d
Mysql 表结构变更方案对比及分析
没有 accept,建立 TCP 连接,可以吗?
shell脚本基础语句使用(一)
开源一夏 | 基于若依架构的列表详情展示
What is the Treasure Project (TPC), a dark horse with wings in 2022!
基于AWS构建云上数仓第一步:云平台的基础概念