力扣1019.链表中的下一个更大节点

力扣1019.链表中的下一个更大节点

  • 从左到右

    • 每个数确定下一个更大节点后 弹出
    • 栈中存下标 即res.size()
cpp 复制代码
  class Solution {
  public:
      vector<int> nextLargerNodes(ListNode* head) {
          vector<int> res;
          stack<int> st;
          for(auto i=head;i;i=i->next)
          {
              while(!st.empty() && res[st.top()] < i->val)
              {
                  res[st.top()] = i->val;
                  st.pop();
              }
              st.emplace(res.size());
              res.push_back(i->val);
          }
          while(!st.empty())
          {
              res[st.top()] = 0;
              st.pop();
          }
          return res;
      }
  };
相关推荐
y = xⁿ1 分钟前
【LeetCodehot100】T23:合并k个升序链表
java·数据结构·链表
做怪小疯子13 分钟前
Leetcode刷题——8.重叠区间
算法·leetcode·职场和发展
2401_8578652316 分钟前
C++模块接口设计
开发语言·c++·算法
add45a26 分钟前
嵌入式C++低功耗设计
开发语言·c++·算法
DeepModel28 分钟前
【概率分布】指数分布(Exponential Distribution)原理、推导与实战
python·算法·概率论
_饭团33 分钟前
指针核心知识:5篇系统梳理3
c语言·数据结构·算法·leetcode·面试·学习方法·改行学it
2401_8747325335 分钟前
C++中的状态模式
开发语言·c++·算法
BB学长35 分钟前
LBM vs FVM:谁才是 CFD 的未来?
人工智能·算法·机器学习
闻缺陷则喜何志丹36 分钟前
【枚举】P6786「SWTR-6」GCDs & LCMs|普及+
c++·算法·洛谷
m0_716667071 小时前
实时数据压缩库
开发语言·c++·算法