力扣636.函数的独占时间

力扣636.函数的独占时间

  • 用一个对组栈存每个start数据,first为编号,second为开始时间

    • start时:若栈为空 直接入栈
      • 若不为空 处理前一段已独占的时间
    • end时:将自己的独占时间处理了
      • 若栈非空 将栈顶元素的开始时间更新为当前时间+1
      • 因为前面所有时间都已处理过 不用管了
  •   class Solution {
      public:
          vector<int> exclusiveTime(int n, vector<string>& logs) {
              vector<int> ans(n,0);
              stack<pair<int,int>> st;
              for(auto t:logs)
              {
                  int num=0;
                  int idx=0;
                  while(t[idx] != ':')
                  {
                      num = num * 10 + t[idx] - '0';
                      idx ++;
                  }
                  idx ++;
                  if(t[idx] == 's')
                  {
                      idx += 6;
                      int ti = 0;
                      while(idx < t.size())
                      {
                          ti = ti * 10 + t[idx] - '0';
                          idx ++;
                      }
                      if(st.empty()) st.push({num,ti});
                      else
                      {
                          ans[st.top().first] += ti - st.top().second;
                          st.push({num,ti});
                      }
                  }
                  else
                  {
                      idx += 4;
                      int ti = 0;
                      while(idx < t.size())
                      {
                          ti = ti * 10 + t[idx] - '0';
                          idx ++;
                      }
      
                      ans[st.top().first] += ti - st.top().second + 1;
                      st.pop();
                      if(!st.empty()) st.top().second = ti + 1;
                  }
              }
              return ans;
          }
      };
    

相关推荐

  1. 636.函数独占时间

    2024-07-13 02:02:03       20 阅读
  2. 2594.修车最少时间

    2024-07-13 02:02:03       53 阅读
  3. 2594.修车最少时间

    2024-07-13 02:02:03       26 阅读
  4. 637. 二叉树层平均值

    2024-07-13 02:02:03       52 阅读
  5. 【每日一题】:修车最少时间

    2024-07-13 02:02:03       54 阅读
  6. 981.基于时间键值存储

    2024-07-13 02:02:03       25 阅读
  7. 2187.完成旅途最少时间

    2024-07-13 02:02:03       35 阅读
  8. 【刷爆637. 二叉树层平均值】

    2024-07-13 02:02:03       26 阅读
  9. labuladong——一刷day66

    2024-07-13 02:02:03       47 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 02:02:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 02:02:03       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 02:02:03       58 阅读
  4. Python语言-面向对象

    2024-07-13 02:02:03       69 阅读

热门阅读

  1. day19打卡

    2024-07-13 02:02:03       16 阅读
  2. 【qml学习笔记】在qml中连接信号与槽

    2024-07-13 02:02:03       22 阅读
  3. 【第20章】MyBatis-Plus逻辑删除支持

    2024-07-13 02:02:03       13 阅读
  4. AI agents 印象

    2024-07-13 02:02:03       18 阅读
  5. Kafka 面试题精选

    2024-07-13 02:02:03       23 阅读
  6. gpt讲 Observable 对象

    2024-07-13 02:02:03       21 阅读