C++ | Leetcode C++题解之第496题下一个更大元素I

题目:

题解:

cpp 复制代码
class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
        unordered_map<int,int> hashmap;
        stack<int> st;
        for (int i = nums2.size() - 1; i >= 0; --i) {
            int num = nums2[i];
            while (!st.empty() && num >= st.top()) {
                st.pop();
            }
            hashmap[num] = st.empty() ? -1 : st.top();
            st.push(num);
        }
        vector<int> res(nums1.size());
        for (int i = 0; i < nums1.size(); ++i) {
            res[i] = hashmap[nums1[i]];
        }
        return res;
    }
};
相关推荐
在水一缸1 小时前
深入浅出 Catch2:现代 C++ 测试框架的优雅实践
开发语言·c++·单元测试·log4j·测试框架·catch2
良木林1 小时前
滑动窗口 - LeetCode hot 100
javascript·算法·leetcode·双指针·滑动窗口
2401_841495641 小时前
【数据结构】B*树
数据结构·c++·b树·算法·删除·插入·三分分裂
斐夷所非2 小时前
在纷繁竞逐中稳步前行:C++ 2006–2020
c++
保持清醒5402 小时前
类和对象上
c++
乱七八糟的屋子2 小时前
Poco C++高级实战教程:线程池+日志系统+加密算法+WebSocket长连接
c++·websocket·加密解密·#poco·#c++高级开发
hold?fish:palm3 小时前
7 接雨水
开发语言·c++·leetcode
流浪0014 小时前
数据结构篇(五):线性表——栈
数据结构·c++·算法
郝学胜-神的一滴4 小时前
中级OpenGL教程 022:探秘三维世界的血脉传承——物体父子关系与矩阵递归奥义
c++·线性代数·unity·矩阵·游戏引擎·unreal engine·opengl
tkevinjd4 小时前
力扣148-排序链表
算法·leetcode·链表