LeetCode:496. 下一个更大元素 I

class Solution {

public:

vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {

vector<int>greater=nextGreaterElement(nums2);

unordered_map<int,int>greaterMap;

for(int i=0;i<nums2.size();i++){

greaterMap[nums2[i]]=greater[i];

}

vector<int>res(nums1.size());

for(int i=0;i<nums1.size();i++){

res[i]=greaterMap[nums1[i]];

}

return res;

}

vector<int> nextGreaterElement(vector<int>& nums){

int n=nums.size();

vector<int>res(n);

stack<int> s;

for(int i=n-1;i>=0;i--){

while(!s.empty()&&s.top()<=nums[i]){

s.pop();

}

res[i]=s.empty()?-1:s.top();

s.push(nums[i]);

}

return res;

}

};

相关推荐
bkspiderx2 小时前
C++设计模式之行为型模式:职责链模式(Chain of Responsibility)
c++·设计模式·责任链模式
勇闯逆流河3 小时前
【C++】AVL详解
开发语言·c++
未知陨落3 小时前
LeetCode:78.跳跃游戏
算法·leetcode
Wenhao.3 小时前
LeetCode-Hot100 最小栈实现
算法·leetcode
长安——归故李3 小时前
【PLC程序学习】
java·c语言·javascript·c++·python·学习·php
闲人编程3 小时前
会议安排问题之贪心算法
python·算法·ios·贪心算法·会议问题·算法改进·codecapsule
青草地溪水旁3 小时前
设计模式(C++)详解——迭代器模式(4)
c++·设计模式·迭代器模式
叫我詹躲躲3 小时前
🚀 震撼!10道DFS&BFS神级题目让你的算法能力飙升300%
前端·leetcode
Jiezcode4 小时前
LeetCode 48. 旋转图像
c++·算法·leetcode·职场和发展