力扣503.下一个更大元素II
-
循环数组实现:断环成链
- 开二倍数组做单调栈
-
class Solution { public: vector<int> nextGreaterElements(vector<int>& nums) { int n = nums.size(); vector<int> res(n); stack<int> st; for(int i =2*n-1;i>=0;i--) { while(!st.empty() && st.top() <= nums[i%n]) st.pop(); if(!st.empty()) res[i%n] = st.top(); else res[i%n] = -1; st.push(nums[i%n]); } return res; } };