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 小时前
Windows MFC添加类,变量,类导向
c++·windows·mfc
yudiandian20141 小时前
MFC - 使用 Base64 对图片进行加密解密
c++·mfc
yudiandian20141 小时前
MFC - Picture Control 控件显示图片
c++·mfc
我是李武涯6 小时前
从`std::mutex`到`std::lock_guard`与`std::unique_lock`的演进之路
开发语言·c++
夏鹏今天学习了吗7 小时前
【LeetCode热题100(59/100)】分割回文串
算法·leetcode·深度优先
卡提西亚7 小时前
C++笔记-10-循环语句
c++·笔记·算法
还是码字踏实7 小时前
基础数据结构之数组的双指针技巧之对撞指针(两端向中间):三数之和(LeetCode 15 中等题)
数据结构·算法·leetcode·双指针·对撞指针
亮剑20187 小时前
第1节:C语言初体验——环境、结构与基本数据类型
c++
William_wL_8 小时前
【C++】类和对象(下)
c++
William_wL_8 小时前
【C++】内存管理
c++