zoukankan      html  css  js  c++  java
  • 503. Next Greater Element II

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

    class Solution {
    public:
        vector<int> nextGreaterElements(vector<int>& nums) {
            stack<int> st;
            int n = nums.size();
            for (int i = 2 * n - 1; i >= n; i--) {
                int cur = nums[i % n];
                while (!st.empty() && cur >= st.top())
                    st.pop();
                st.push(cur);
            }
            
            vector<int> res(n, 0);
            for (int i = n - 1; i >= 0; i--) {
                int cur = nums[i];
                while (!st.empty() && cur >= st.top())
                    st.pop();
                if (st.empty())
                    res[i] = -1;
                else
                    res[i] = st.top();
                st.push(cur);
            }
            
            return res;
        }
    };
  • 相关阅读:
    164-268. 丢失的数字
    163-20. 有效的括号
    Sword 30
    Sword 29
    Sword 27
    Sword 25
    Sword 24
    Sword 22
    Sword 21
    Sword 18
  • 原文地址:https://www.cnblogs.com/JTechRoad/p/8990477.html
Copyright © 2011-2022 走看看