On a single threaded CPU, we execute some functions.  Each function has a unique id between 0 and N-1.

We store logs in timestamp order that describe when a function is entered or exited.

Each log is a string with this format: "{function_id}:{"start" | "end"}:{timestamp}".  For example, "0:start:3" means the function with id 0started at the beginning of timestamp 3.  "1:end:2" means the function with id 1ended at the end of timestamp 2.

A function's exclusive time is the number of units of time spent in this function.  Note that this does not include any recursive calls to child functions.

The CPU is single threaded which means that only one function is being executed at a given time unit.

Return the exclusive time of each function, sorted by their function id.

分析:

因为开始和结束永远是一对的。而且如果当前是结束的话,之前一个一定是开始。所以,我们用stack来存之前的log,拿到end就pop,然后更新当前function的执行时间以及调用这个function

的执行时间。

 class Solution {
public int[] exclusiveTime(int n, List<String> logs) {
Stack<Log> stack = new Stack<>();
int[] result = new int[n];
for (String content : logs) {
Log log = new Log(content);
if (log.isStart) {
stack.push(log);
} else {
Log top = stack.pop();
int runningTime = log.time - top.time + ;
result[top.id] += runningTime;
if (!stack.isEmpty()) {
result[stack.peek().id] -= runningTime;
}
}
}
return result;
} public static class Log {
public int id;
public boolean isStart;
public int time; public Log(String content) {
String[] strs = content.split(":");
id = Integer.valueOf(strs[]);
isStart = strs[].equals("start");
time = Integer.valueOf(strs[]);
}
}
}

Exclusive Time of Functions的更多相关文章

  1. Leetcode 之 Exclusive Time of Functions

    636. Exclusive Time of Functions 1.Problem Given the running logs of n functions that are executed i ...

  2. [LeetCode] Exclusive Time of Functions 函数的独家时间

    Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...

  3. [Swift]LeetCode636. 函数的独占时间 | Exclusive Time of Functions

    Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...

  4. [leetcode]636. Exclusive Time of Functions函数独占时间

    Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...

  5. 636. Exclusive Time of Functions 进程的执行时间

    [抄题]: Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU ...

  6. [LeetCode] 636. Exclusive Time of Functions 函数的独家时间

    Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...

  7. 【LeetCode】636. Exclusive Time of Functions 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...

  8. 模拟函数调用 Simulation Exclusive Time of Functions

    2018-04-28 14:10:33 问题描述: 问题求解: 个人觉得这是一条很好的模拟题,题目大意就是给了一个单线程的处理器,在处理器上跑一个函数,但是函数里存在调用关系,可以是调用其他函数,也可 ...

  9. 636. Exclusive Time of Functions

    // TODO: need improve!!! class Log { public: int id; bool start; int timestamp; int comp; // compasa ...

随机推荐

  1. 【Java Web】IDEA如何创建及配置Web项目(多图)

    正文之前 在学习Java Web时,第一个遇到的问题就是如何创建或配置Web项目了,今天,就用IntelliJ IDEA 来进行Web项目配置: 创建Web项目 配置web项目 正文 创建Web项目 ...

  2. jquery unbind()方法 语法

    jquery unbind()方法 语法 作用:unbind() 方法移除被选元素的事件处理程序.该方法能够移除所有的或被选的事件处理程序,或者当事件发生时终止指定函数的运行.ubind() 适用于任 ...

  3. Hibernate 5 开始使用指南前言

    同时在面向对象软件和关系型数据库进行工作,可能会非常复杂和费时.数据在对象和数据库之间可能会不一致,然后导致开发成本会非常高. Hibernate 是一个针对 Java 环境的对象关系映射(Objec ...

  4. apt 和 apt-get的区别

    apt 和 apt-get的区别 - liudsl的博客 - CSDN博客  https://blog.csdn.net/liudsl/article/details/79200134 Linux软件 ...

  5. JQuery属性操作之attr()和prop()的区别

    代码示例: <!doctype html> <html lang="en"> <head> <meta charset="UTF ...

  6. Python3学习笔记(十八):文件上传和下载

    文件上传 以人人网上传头像为例,用Fiddler抓取的上传头像接口报文如下 上传头像图片代码: import requests upload_url = 'http://upload.renren.c ...

  7. Luogu P5564 [Celeste-B]Say Goodbye (多项式、FFT、Burnside引理、组合计数)

    题目链接 https://www.luogu.org/problem/P5564 题解 这题最重要的一步是读明白题. 为了方便起见下面设环长可以是\(1\), 最后统计答案时去掉即可. 实际上就相当于 ...

  8. 白鹭引擎EUI做H5活动 入门篇

    前言:本学习文档的目的是为了实现h5,或者简单的h5游戏,比如说,我们要实现一个可以左右,或者上下移动的场景的h5,在场景移动的过程中,会有相应的动画或者操作,我们通过 js 也可以实现,但是为了流畅 ...

  9. npm+cnpm+vuecli3打包相关

    1,npm install和cnpm install时的不同 https://blog.csdn.net/csm0912/article/details/90264026 2,npm设置和查看仓库源 ...

  10. vue的通信方式(二)---祖父孙三个级别的之间的隔代通信

    在之前的文章中我们提到了vue常用的几种通信方式,如父子,子父,以及兄弟组件之间的通信,可以通过这个传送门了解他们:Vue通信方式(一) 当我们如果遇到祖组件,父组件,孙组件,三个级别嵌套时,我们该怎 ...