题解 | 下一个较大元素II

下一个较大元素II

https://www.nowcoder.com/practice/a0c19f3489774fe693d71490ce83b648

#include <stack>
#include <vector>
class NextElement {
  public:
    vector<int> findNext(vector<int> A, int n) {
        // write code here
        stack<int >stk;
        vector<int> res(n, -1);
        for (int i = n - 1; i >= 0; --i) {
            stack<int> tmp;
            while (!stk.empty() && stk.top() <= A[i] ) {
                tmp.push(stk.top());
                stk.pop();
            }
            if (!stk.empty()) res[i] = stk.top();
            stk.push(A[i]);
            while (!tmp.empty()) {
                stk.push(tmp.top());
                tmp.pop();
            
            }
        }
        return res;
    }
};

全部评论

相关推荐

迷茫的大四🐶:在公司休息?要不是中午迫不得已,谁会在公司休息
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务