栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

力扣:636. 函数的独占时间

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

力扣:636. 函数的独占时间


import java.util.ArrayDeque;
import java.util.Deque;
import java.util.List;


public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();

    }

    public int[] exclusiveTime(int n, List logs) {
        int[] res = new int[n];
        Deque stack = new ArrayDeque<>();
        for (String log : logs) {
            // 运行的线程
            int idx = Integer.parseInt(log.substring(0, log.indexOf(':')));
            // 运行的类型,有两种,一种是start,一种end
            String type = log.substring(log.indexOf(':') + 1, log.lastIndexOf(':') );
            // 对应的时间戳
            int timestamp = Integer.parseInt(log.substring(log.lastIndexOf(':') + 1));
            if (type.equals("start")){
                if (!stack.isEmpty()){
                    // 保存下上一个线程的时间
                    res[stack.peek()[0]] += timestamp - stack.peek()[1];
                }
                // 记录当先线程的开始时间
                stack.push(new int[]{idx, timestamp});
            } else {
                int[] t= stack.poll();
                // 记录下这个线程的运行时间
                res[t[0]] += timestamp - t[1] + 1;
                if (!stack.isEmpty()){
                    stack.peek()[1] = timestamp + 1;
                }
            }
        }
        return res;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1038586.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号