思路:循环,所以从后往前执行两次单调栈
vector<int> nextGreaterElements(vector<int>& nums) {if(nums.empty())return {};int len = nums.size();vector<int> ans(len, 0);stack<int> s;for(int i = len - 1;i >= 0;--i){while(!s.empty() && nums[i] >= s.top())s.pop();if(s.empty())ans[i] = -1;else ans[i] = s.top();s.push(nums[i]);}for(int i = len - 1;i >= 0;--i){while(!s.empty() && nums[i] >= s.top())s.pop();if(s.empty())ans[i] = -1;else ans[i] = s.top();s.push(nums[i]);}return ans;}
