题解 | 下一个较大元素

下一个较大元素

https://www.nowcoder.com/practice/11ae41035eef4ed9b354d0752f5abc6f

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

            }
            index_stk.push(i);
        }
        return res;
    }
};

全部评论

相关推荐

09-14 20:51
四川大学 Java
慢热的鲸鱼在学习:985加粗就行了,第二个项目来不及准备也没事,省的写了问你你还不会。你只需准备面试八股和项目场景,剩下的交给985。即使面不过也没事,面试经验是最重要的,你现在不缺时间
简历中的项目经历要怎么写
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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