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;
    }
};
相关推荐
今天_也很困16 分钟前
LeetCode 热题100-15.三数之和
数据结构·算法·leetcode
Ccjf酷儿29 分钟前
C++语言程序设计 (郑莉)第十章 泛型程序设计与C++标准模板库
开发语言·c++
千金裘换酒44 分钟前
LeetCode 数组经典题刷题
算法·leetcode·职场和发展
alphaTao2 小时前
LeetCode 每日一题 2026/1/12-2026/1/18
python·算法·leetcode
sin_hielo2 小时前
leetcode 2943
数据结构·算法·leetcode
明洞日记3 小时前
【CUDA手册002】CUDA 基础执行模型:写出第一个正确的 Kernel
c++·图像处理·算法·ai·图形渲染·gpu·cuda
程序员-King.4 小时前
day134—快慢指针—环形链表(LeetCode-141)
算法·leetcode·链表·快慢指针
Swift社区4 小时前
LeetCode 376 摆动序列
算法·leetcode·职场和发展
oioihoii4 小时前
程序员如何系统入门Vibe Coding?
c++
C+++Python4 小时前
C++类型判断
开发语言·c++