当前位置: 首页 > news >正文

算法训练第五十八天

总结:今日事单调栈的开端,还是挺巧妙的。

496. 下一个更大元素 I - 力扣(LeetCode)

代码:

class Solution {
public:vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {vector<int> res(nums1.size(),-1);unordered_map<int,int> umap;if(nums1.size() == 0)return res;for(int i = 0; i < nums1.size();i++)umap[nums1[i]] = i;stack<int> st;st.push(0);for(int i = 1;i < nums2.size();i++){if(nums2[i] <= nums2[st.top()])st.push(i);else {while(!st.empty() && nums2[i] > nums2[st.top()]){if(umap.count(nums2[st.top()]) > 0){int index = umap[nums2[st.top()]];res[index] = nums2[i];}st.pop();}st.push(i);}}return res;}
};

739. 每日温度 - 力扣(LeetCode)

class Solution {
public:vector<int> dailyTemperatures(vector<int>& temperatures) {stack<int> st;vector<int> res(temperatures.size(),0);st.push(0);for(int i = 1;i < temperatures.size();i++){if(temperatures[i] <= temperatures[st.top()])st.push(i);else {while(!st.empty() && temperatures[i] > temperatures[st.top()]){res[st.top()] = i - st.top();st.pop();}st.push(i);}}return res;}
};

http://www.lryc.cn/news/158999.html

相关文章:

  • 如何快速生成一个H5滑动的卡片(单页和分页都有)
  • 嵌入式开发笔试面试
  • 2023国赛数学建模B题思路分析 - 多波束测线问题
  • thinkphp6 入门(5)-- 模型是什么 怎么用
  • Hadoop HDFS 高阶优化方案
  • 通俗易懂讲解大模型:Tokenizer
  • nested exception is java.io.FileNotFoundException
  • ARM编程模型-常用指令集
  • MAC M2芯片执行yolov8 + deepsort 实现目标跟踪
  • 使用Python轻松实现文档编写
  • 前后端分离项目,整合成jar包,刷新404或空白页,解决方法
  • 前端、后端面试集锦
  • Web存储
  • 字节对齐(C++,C#)
  • 使用mybatisplus查询sql时,报Error attempting to get column ‘ID‘ from result set错误
  • ElementUI浅尝辄止32:NavMenu 导航菜单
  • @Value的注入与静态注入 与 组件中静态工具类的注入
  • Qt--自定义搜索控件,QLineEdit带前缀图标
  • 8月AI实战:工业视觉缺陷检测
  • Kubernetes的ExternalName详解
  • 使用 Pandera 的 PySpark 应用程序的数据验证
  • README
  • Excel周报制作
  • Qt QtCreator 所有官方下载地址
  • C++包含整数各位重组
  • 数学建模--模型总结(5)
  • JavaScript 中的原型到底该如何理解?
  • 【MySQL基础】事务隔离03
  • 2023高教社杯数学建模C题思路分析 - 蔬菜类商品的自动定价与补货决策
  • 【MySQL】初见数据库