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的博客

相关文章:

  • 2021-12-08
  • 2022-03-07
  • 2021-11-14
猜你喜欢
  • 2021-08-03
  • 2022-01-28
  • 2021-05-16
  • 2022-01-20
  • 2022-02-13
  • 2022-12-23
  • 2021-11-17
相关资源
相似解决方案