zoukankan      html  css  js  c++  java
  • 496. Next Greater Element I

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

    class Solution {
    public:
        vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
            unordered_map<int,int> m;
            stack<int> st;
            for (int i = nums.size() - 1; i >= 0; i--) {
                int cur = nums[i];
                while (!st.empty() && cur >= st.top())
                    st.pop();
                if (st.empty())
                    m[cur] = -1;
                else
                    m[cur] = st.top();
                st.push(cur);
            }
            
            vector<int> res;
            for (auto i : findNums)
                res.push_back(m[i]);
            return res;
        }
    };
  • 相关阅读:
    CSP2018-09
    CSP2018-03
    CSP2017-12
    CSP2017-09
    CSP2017-03
    CSP2016-12
    [算法设计与分析] 奶酪 (并查集)
    5555
    阿超
    结对作业
  • 原文地址:https://www.cnblogs.com/JTechRoad/p/8990390.html
Copyright © 2011-2022 走看看