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;
    }
};
相关推荐
今晚打老虎39 分钟前
c++之基础A(二维数组)第四课
开发语言·c++
君义_noip1 小时前
信息学奥赛一本通 1615:【例 1】序列的第 k 个数
c++·算法·信息学奥赛·csp-s
AA陈超1 小时前
虚幻引擎5 GAS开发俯视角RPG游戏 P07-19.发送鼠标光标数据
c++·笔记·学习·游戏·ue5·虚幻引擎
bybitq1 小时前
Leetcode-3780-Python
python·算法·leetcode
如何原谅奋力过但无声1 小时前
【力扣-Python-75】颜色分类(middle)
python·算法·leetcode
玖剹1 小时前
哈希表相关题目
数据结构·c++·算法·leetcode·哈希算法·散列表
暗然而日章2 小时前
C++基础:Stanford CS106L学习笔记 14 类型安全 & `std::optional`
c++·笔记·学习
L_09072 小时前
【C++】高阶数据结构 -- 二叉搜索树(BST)
数据结构·c++
大筒木老辈子2 小时前
C++笔记---并发支持库(future)
java·c++·笔记
PyGata2 小时前
CMake学习笔记(二):CMake拷贝文件夹
c++·笔记·学习