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;
    }
};
相关推荐
学涯乐码堂主4 小时前
GESP C++ 四级第一章:再谈函数(上)
c++·青少年编程·gesp·四级·学漄乐码青少年编程培训
微露清风4 小时前
系统性学习C++-第九讲-list类
c++·学习·list
大佬,救命!!!4 小时前
C++多线程同步与互斥
开发语言·c++·学习笔记·多线程·互斥锁·同步与互斥·死锁和避免策略
Kuo-Teng5 小时前
Leetcode438. 找到字符串中所有字母异位词
java·算法·leetcode
散峰而望5 小时前
C++入门(一)(算法竞赛)
c语言·开发语言·c++·编辑器·github
C_Liu_5 小时前
13.C++:继承
开发语言·c++
墨染点香6 小时前
LeetCode 刷题【138. 随机链表的复制】
算法·leetcode·链表
凡同学。6 小时前
通信人C++自学
c++·应届生秋招·后端四件套
威桑6 小时前
C++ Linux 环境下内存泄露检测方式
linux·c++
报错小能手7 小时前
C++笔记(面向对象)RTTI操作符
开发语言·c++·笔记