496. Next Greater Element I
https://leetcode.com/problems/next-greater-element-i/description/
class Solution { public: vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) { unordered_map<int,int> m; stack<int> st; for (int i = nums.size() - 1; i >= 0; i--) { int cur = nums[i]; while (!st.empty() && cur >= st.top()) st.pop(); if (st.empty()) m[cur] = -1; else m[cur] = st.top(); st.push(cur); } vector<int> res; for (auto i : findNums) res.push_back(m[i]); return res; } };
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步