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;
    }
};
相关推荐
TonyLee01719 小时前
LLVM安装(ubuntu22)
c++
Swift社区20 小时前
LeetCode 465 最优账单平衡
算法·leetcode·职场和发展
weixin_4450547220 小时前
力扣热题51
c++·python·算法·leetcode
smj2302_796826521 天前
解决leetcode第3801题合并有序列表的最小成本
数据结构·python·算法·leetcode
汉克老师1 天前
GESP2025年12月认证C++七级真题与解析(单选题8-15)
c++·dfs·bfs·二分·强联通分量·gesp7级·gesp七级
fqbqrr1 天前
2601C++,pmr管理内存
c++
君义_noip1 天前
【模板:矩阵加速递推】信息学奥赛一本通 1642:【例 2】Fibonacci 第 n 项
c++·线性代数·矩阵·信息学奥赛·csp-s
宠..1 天前
优化文件结构
java·服务器·开发语言·前端·c++·qt
编程之路,妙趣横生1 天前
C++11(上)
c++
微露清风1 天前
系统性学习C++-第十六讲-AVL树实现
java·c++·学习