leetcode 单调栈

https://leetcode-cn.com/problems/next-greater-element-ii/submissions/

class Solution {
public:
    vector<int> nextGreaterElements(vector<int>& nums) {
        int n = nums.size();
        vector<int> ret(n, -1);
        stack<int> stk;
        for (int i = 0; i < n * 2 - 1; i++) {
            while (!stk.empty() && nums[stk.top()] < nums[i % n]) {
                ret[stk.top()] = nums[i % n];
                stk.pop();
            }
            stk.push(i % n);
        }
        return ret;
    }
};

给一篇单调栈dl的博客

为了自己,和那些爱你的人
原文地址:https://www.cnblogs.com/zhmlzhml/p/14489591.html