news 2026/11/5 2:39:22

单调栈学习C++

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
单调栈学习C++

【题目描述】

对于给定的一个temperatures数组,每个元素表示当天的温度。对于每天的温度,求出下一次更高的温度出现在几天后。

【算法】单调栈

我们可以维护一个栈结构,先将数组的首元素入栈,然后开始遍历这个数组,如果遍历到的数组元素比栈顶元素小,那么就入栈;如果相等,也入栈;当遍历到的数组元素比此时栈顶的元素大时,记录此时相隔的天数,然后将栈顶元素弹出,继续比较栈顶的元素和数组元素的大小,直到栈顶元素小于或者等于栈顶元素,此时将该数组元素入栈。然后接着遍历下一个数组元素,依次循环。

由于题目中求的是相隔的天数,所以我们不需要将数组元素入栈,只需入栈该元素的下标即可,这样就可以直接计算相隔的天数:数组元素对应的下标减去栈顶元素对应的下标,就是该栈顶元素与下一个更高温度相隔的天数。

图示:(以题目中的示例1为例)

可以发现,栈中的元素(下标对应的元素)始终保持单调递减(从栈底到栈顶),因此成为单调栈。

【代码】

代码语言:javascript

AI代码解释

class Solution { public: vector<int> dailyTemperatures(vector<int>& temperatures) { int n=temperatures.size(); vector<int> ans(n,0); stack<int> st;//存储下标 st.push(0); for(int i=1;i<n;i++) { //遇到温度大于栈顶的,就一直出栈,保持递减性 while(!st.empty()&&temperatures[st.top()]<temperatures[i]) { int index=st.top(); st.pop(); ans[index]=i-index; } st.push(i); } return ans; } };

时间复杂度O(N),每个元素最多入栈依次。空间复杂度O(N)

二,下一个更大的元素I

【题目描述】

本题存在两个数组,nums1是nums2的子集,对于nums1数组的每个元素,求出这些元素在对应nums2数组中的下一个更大的元素。如果不存在,则为-1

【算法】单调栈

本题与上一题的思路一样,只不过加了一点要求。

求当前元素的下一个更大的元素,就需要维护一个单调栈(单调递减)。

本题还是求数组nums2上每个元素的下一个更大的元素是多少。所以还是在nums2数组上使用单调栈。和上题的过程一样,只不过在判断的时候,还需加上一个条件。

假设找到了当前栈顶的下一个更大元素k,还需判断栈顶元素是否在nums1中出现过,如果该元素在nums1中出现过,那么就将k记录在最终结果数组ans中,不过还需要保证和nums1数组的位置一一对应。

所以可以做下预处理工作,将数组nums1中的元素和下标使用哈希表保存起来。


www.dongchedi.com/article/7593851148961301016
www.dongchedi.com/article/7593850104424153662
www.dongchedi.com/article/7593850176729661977
www.dongchedi.com/article/7593846832837476926
www.dongchedi.com/article/7593845728238240318
www.dongchedi.com/article/7593844734922310169
www.dongchedi.com/article/7593844623060582936
www.dongchedi.com/article/7593844334273806873
www.dongchedi.com/article/7593843358104126014
www.dongchedi.com/article/7593842809959186969
www.dongchedi.com/article/7593841312735265305
www.dongchedi.com/article/7593843343910502937
www.dongchedi.com/article/7593842372367696446
www.dongchedi.com/article/7593840416609665561
www.dongchedi.com/article/7593840780297519678
www.dongchedi.com/article/7593839386396377625
www.dongchedi.com/article/7593834104484266520
www.dongchedi.com/article/7593832795995603518
www.dongchedi.com/article/7593830981627494974
www.dongchedi.com/article/7593832368020996670
www.dongchedi.com/article/7593831490098922008
www.dongchedi.com/article/7593829122569273881
www.dongchedi.com/article/7593830609634476606
www.dongchedi.com/article/7593827739870921241
www.dongchedi.com/article/7593829351788429848
www.dongchedi.com/article/7593829210335281688
www.dongchedi.com/article/7593823428667294233
www.dongchedi.com/article/7593823195556725272
www.dongchedi.com/article/7593823897800409662
www.dongchedi.com/article/7593821698383217214
www.dongchedi.com/article/7593823146111238718
www.dongchedi.com/article/7593812815950266942

www.dongchedi.com/article/7593861948883010110
www.dongchedi.com/article/7593860184296997401
www.dongchedi.com/article/7593857995939824190
www.dongchedi.com/article/7593858058859037208
www.dongchedi.com/article/7593856188576465432
www.dongchedi.com/article/7593856799745524286
www.dongchedi.com/article/7593854395277001241
www.dongchedi.com/article/7593853248357089816
www.dongchedi.com/article/7593849479917847102
www.dongchedi.com/article/7593850791879016985
www.dongchedi.com/article/7593849102899757593
www.dongchedi.com/article/7593846341370102297
www.dongchedi.com/article/7593845182169268761
www.dongchedi.com/article/7593844699736621592
www.dongchedi.com/article/7593846059231855166
www.dongchedi.com/article/7593845002074587673
www.dongchedi.com/article/7593845199110029886
www.dongchedi.com/article/7593844109958234648
www.dongchedi.com/article/7593843358104584766
www.dongchedi.com/article/7593841598811832894
www.dongchedi.com/article/7593843060308967998
www.dongchedi.com/article/7593841492519715353
www.dongchedi.com/article/7593841963301012030
www.dongchedi.com/article/7593838407353729561
www.dongchedi.com/article/7593831928227267096
www.dongchedi.com/article/7593830985880879641
www.dongchedi.com/article/7593832895307973182
www.dongchedi.com/article/7593828800342360601
www.dongchedi.com/article/7593829728961020441
www.dongchedi.com/article/7593829195726258712
www.dongchedi.com/article/7593829391919202841
www.dongchedi.com/article/7593829122569306649
www.dongchedi.com/article/7593828972870271550
www.dongchedi.com/article/7593826313157820953
www.dongchedi.com/article/7593822015514313241
www.dongchedi.com/article/7593824624384180760
www.dongchedi.com/article/7593822602171646488
www.dongchedi.com/article/7593822015513952793
www.dongchedi.com/article/7593822160830218776
www.dongchedi.com/article/7593812639475155518
www.dongchedi.com/article/7593862391214047806
www.dongchedi.com/article/7593856670141284889
www.dongchedi.com/article/7593857060190798398
www.dongchedi.com/article/7593856931858022937
www.dongchedi.com/article/7593854937294275096
www.dongchedi.com/article/7593853687676715582
www.dongchedi.com/article/7593852941996393022
www.dongchedi.com/article/7593851894985081368
www.dongchedi.com/article/7593850569647997502
www.dongchedi.com/article/7593848374730899993
www.dongchedi.com/article/7593846886513738265
www.dongchedi.com/article/7593846450732483097
www.dongchedi.com/article/7593846491957985854
www.dongchedi.com/article/7593843364807148056
www.dongchedi.com/article/7593843093477605913
www.dongchedi.com/article/7593842919485276696
www.dongchedi.com/article/7593843735868637721
www.dongchedi.com/article/7593841312735003161
www.dongchedi.com/article/7593843018189488702
www.dongchedi.com/article/7593840609178370585
www.dongchedi.com/article/7593840851324420633
www.dongchedi.com/article/7593836770471625241
www.dongchedi.com/article/7593833071250948670
www.dongchedi.com/article/7593831056378790462
www.dongchedi.com/article/7593830088668906009
www.dongchedi.com/article/7593830311700988478
www.dongchedi.com/article/7593831918585004568
www.dongchedi.com/article/7593830135552311833
www.dongchedi.com/article/7593829391919170073
www.dongchedi.com/article/7593830256495542808
www.dongchedi.com/article/7593828787180388888
www.dongchedi.com/article/7593829247186223678
www.dongchedi.com/article/7593823622788186648
www.dongchedi.com/article/7593824480053559832
www.dongchedi.com/article/7593824197931975192
www.dongchedi.com/article/7593822824964358718
www.dongchedi.com/article/7593821230626013720
www.dongchedi.com/article/7593797606556910104
www.dongchedi.com/article/7593862164004209176
www.dongchedi.com/article/7593859135339479576
www.dongchedi.com/article/7593858557611803160
www.dongchedi.com/article/7593857292345590334
www.dongchedi.com/article/7593856001158332953
www.dongchedi.com/article/7593854938854375961
www.dongchedi.com/article/7593853320968585790
www.dongchedi.com/article/7593850791878951449
www.dongchedi.com/article/7593849985222230553
www.dongchedi.com/article/7593850714343424536
www.dongchedi.com/article/7593845458314297880
www.dongchedi.com/article/7593845882341376574
www.dongchedi.com/article/7593845435790836249
www.dongchedi.com/article/7593844887007593022
www.dongchedi.com/article/7593844459406852670
www.dongchedi.com/article/7593842809959285273
www.dongchedi.com/article/7593843735868834329
www.dongchedi.com/article/7593843853976027710
www.dongchedi.com/article/7593840934157566489
www.dongchedi.com/article/7593842500343906841
www.dongchedi.com/article/7593843018189292094
www.dongchedi.com/article/7593841511696155198
www.dongchedi.com/article/7593840009971548696
www.dongchedi.com/article/7593832297598960152
www.dongchedi.com/article/7593832634816578073
www.dongchedi.com/article/7593830981627363902

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/11/2 2:55:26

绝区零自动化工具终极指南:高效智能辅助系统详解

绝区零自动化工具终极指南&#xff1a;高效智能辅助系统详解 【免费下载链接】ZenlessZoneZero-OneDragon 绝区零 一条龙 | 全自动 | 自动闪避 | 自动每日 | 自动空洞 | 支持手柄 项目地址: https://gitcode.com/gh_mirrors/ze/ZenlessZoneZero-OneDragon 绝区零作为一款…

作者头像 李华
网站建设 2026/11/4 5:15:20

Lumafly:空洞骑士模组管理的终极免费解决方案

Lumafly&#xff1a;空洞骑士模组管理的终极免费解决方案 【免费下载链接】Lumafly A cross platform mod manager for Hollow Knight written in Avalonia. 项目地址: https://gitcode.com/gh_mirrors/lu/Lumafly 在《空洞骑士》的深邃世界中&#xff0c;模组为游戏注…

作者头像 李华
网站建设 2026/11/4 10:46:00

PDF-Extract-Kit优化技巧:减少PDF解析内存占用的方法

PDF-Extract-Kit优化技巧&#xff1a;减少PDF解析内存占用的方法 1. 背景与挑战&#xff1a;PDF智能提取中的内存瓶颈 1.1 PDF-Extract-Kit 工具箱简介 PDF-Extract-Kit 是由开发者“科哥”基于开源生态二次开发构建的一款PDF智能内容提取工具箱&#xff0c;集成了布局检测、…

作者头像 李华
网站建设 2026/11/2 2:58:24

虚拟显示器革命:零硬件成本打造专业级多屏工作环境

虚拟显示器革命&#xff1a;零硬件成本打造专业级多屏工作环境 【免费下载链接】parsec-vdd ✨ Virtual super display, upto 4K 2160p240hz &#x1f60e; 项目地址: https://gitcode.com/gh_mirrors/pa/parsec-vdd 在数字生产力日益重要的今天&#xff0c;显示空间的限…

作者头像 李华
网站建设 2026/11/2 2:58:15

一文说清设备树中断属性与外设连接

一文讲透设备树中的中断系统&#xff1a;从外设到CPU的完整链路解析你有没有遇到过这样的情况&#xff1f;硬件工程师拍着胸脯说“按键电路没问题”&#xff0c;可你在板子上按破手指&#xff0c;系统就是没反应。/proc/interrupts里对应的计数器纹丝不动&#xff0c;日志里也看…

作者头像 李华
网站建设 2026/11/5 2:12:46

Jellyfin豆瓣插件终极指南:3步实现媒体库元数据自动化管理

Jellyfin豆瓣插件终极指南&#xff1a;3步实现媒体库元数据自动化管理 【免费下载链接】jellyfin-plugin-douban Douban metadata provider for Jellyfin 项目地址: https://gitcode.com/gh_mirrors/je/jellyfin-plugin-douban 还在为Jellyfin媒体库中那些只有文件名没有…

作者头像 李华