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;
    }
};
相关推荐
小丑西瓜6661 小时前
CMake基础用法,cmake_minimum_required,project,add_executable
linux·服务器·c++·camke
晚风吹长发1 小时前
初步了解Linux中的命名管道及简单应用和简单日志
linux·运维·服务器·开发语言·数据结构·c++·算法
fpcc1 小时前
设计心得——隔离隐藏的初步实践
c++
圣保罗的大教堂1 小时前
leetcode 3315. 构造最小位运算数组 II 中等
leetcode
C++ 老炮儿的技术栈2 小时前
不调用C++/C的字符串库函数,编写函数strcpy
c语言·开发语言·c++·windows·git·postman·visual studio
fyzy2 小时前
C++写后端实现,实现前后端分离
开发语言·c++
Anastasiozzzz2 小时前
leetcode力扣hot100困难题--4.俩个正序数列的中位数
java·算法·leetcode·面试·职场和发展
CSDN_RTKLIB2 小时前
C++谓词
c++·stl
Tisfy3 小时前
LeetCode 3510.移除最小数对使数组有序 II:有序集合
算法·leetcode·题解·设计·有序集合
汉克老师3 小时前
GESP2025年9月认证C++五级真题与解析(单选题9-15)
c++·算法·贪心算法·排序算法·归并排序·gesp5级·gesp五级