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;
    }
};
相关推荐
lvwangshu2 小时前
方阵游戏 题解
题解·技巧·前缀优化·性质题·思维好题·推导题
丰色木夕2 小时前
Ubuntu22.04下源码编译安装USD-Brep
c++·ubuntu·nvidia·usd-brep
文丑颜不良啊2 小时前
LeetCode51~75题目解析
leetcode
学电子她就能回来吗2 小时前
LeetCode Hot 100|普通数组专题学习笔记(零基础版)
笔记·算法·leetcode
衍玊4 小时前
多路转接原理与应用
c++
himobrinehacken6 小时前
位运算揭秘:左右移与符号位陷阱
c语言·c++·算法·安全
文丑颜不良啊7 小时前
LeetCode1~50题目解析
leetcode
Pniubi7 小时前
力扣146.LRU缓存机制
算法·leetcode·缓存
Android小码家9 小时前
Vehicle 实战 自定义属性+测试demo
java·c++·framework·vhal·vehicle
hetao17338379 小时前
2026-10-19 hetao1733837 的刷题记录
c++·算法