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;
    }
};
相关推荐
Tisfy6 分钟前
LeetCode 1291.顺次数:打表/枚举
算法·leetcode·题解·枚举·遍历
ysa05103035 分钟前
【板子】拓扑排序
c++·算法·图论·板子
某不知名網友1 小时前
项目:轻量级搜索引擎
c++
罗超驿1 小时前
双指针算法详解:从入门到精通(Java版)
算法·leetcode·职场和发展
tkevinjd1 小时前
力扣300-最长递增子序列
算法·leetcode·职场和发展·动态规划·贪心
A_humble_scholar3 小时前
Linux(十七)深入多线程编程:同步原语与实战指南
linux·运维·c++
hehelm4 小时前
Linux网络编程—HTTP静态文件服务器
linux·服务器·开发语言·网络·c++
库克克4 小时前
【C++】类和对象--this指针详解
java·开发语言·c++
ch0sen1pm4 小时前
加班之余从零写了个 spdlog 风格的日志库
c++
吃着火锅x唱着歌4 小时前
Effective C++ 学习笔记 条款33 避免遮掩继承来的名称
c++·笔记·学习