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

Each function has a unique id, start from 0 to n-1. A function may be called recursively or by another function.

A log is a string has this format : function_id:start_or_end:timestamp. For example, "0:start:0"means function 0 starts from the very beginning of time 0. "0:end:0" means function 0 ends to the very end of time 0.

Exclusive time of a function is defined as the time spent within this function, the time spent by calling other functions should not be considered as this function's exclusive time. You should return the exclusive time of each function sorted by their function id.

Example 1:

Input:
n = 2
logs =
["0:start:0",
"1:start:2",
"1:end:5",
"0:end:6"]
Output:[3, 4]
Explanation:
Function 0 starts at time 0, then it executes 2 units of time and reaches the end of time 1.
Now function 0 calls function 1, function 1 starts at time 2, executes 4 units of time and end at time 5.
Function 0 is running again at time 6, and also end at the time 6, thus executes 1 unit of time.
So function 0 totally execute 2 + 1 = 3 units of time, and function 1 totally execute 4

思路

这题最难理解的地方是

the very end of 5  =  the very beginning of 6

弄清这点,代码就好理解了

代码

 class Solution {
public int[] exclusiveTime(int n, List<String> logs) {
int[] res = new int[n];
Stack<Integer> stack = new Stack();
int pre = ;
for (String log : logs) {
String[] arr = log.split(":");
// function_id:start_or_end:timestamp
if (arr[].equals("start")) {
if (!stack.isEmpty()) {
res[stack.peek()] += Integer.parseInt(arr[]) - pre;
}
stack.push(Integer.parseInt(arr[]));
pre = Integer.parseInt(arr[]);
} else {
res[stack.pop()] += Integer.parseInt(arr[]) - pre + ;
pre = Integer.parseInt(arr[]) + ;
}
}
return res;
}
}

[leetcode]636. Exclusive Time of Functions函数独占时间的更多相关文章

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

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

  2. Leetcode 之 Exclusive Time of Functions

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

  3. [LeetCode] 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 解题报告(Python)

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

  5. 【leetcode】636. Exclusive Time of Functions

    题目如下: 解题思路:本题和括号匹配问题有点像,用栈比较适合.一个元素入栈前,如果自己的状态是“start”,则直接入栈:如果是end则判断和栈顶的元素是否id相同并且状态是“start”,如果满足这 ...

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

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

  7. 636. Exclusive Time of Functions

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

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

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

  9. Java实现 LeetCode 636 函数的独占时间(栈)

    636. 函数的独占时间 给出一个非抢占单线程CPU的 n 个函数运行日志,找到函数的独占时间. 每个函数都有一个唯一的 Id,从 0 到 n-1,函数可能会递归调用或者被其他函数调用. 日志是具有以 ...

随机推荐

  1. 浅析HttpCient

    HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 java.net 包中已经提供了 ...

  2. javascript创建对象之稳妥构造函数模式(七)

    所谓稳妥对象,指的是没有公共属性,而且其方法也不引用this的对象.稳妥对象最适合在一些安全的环境中(禁止使用this和new)或者在防止数据被其他应用程序改动时. 稳妥构造函数模式有2个特点:1.新 ...

  3. Installation of Scylla on CentOS 7

    Scylla on CentOS 7 Use these steps to install Scylla using Yum repositories on CentOS. Prerequisites ...

  4. 【Python编程:从入门到实践】chapter2 变量和简单数据类型

    2.1 运行2.2 变量 message = "hello" print(message) 2.2.1 变量的命名和使用 2.2.2 使用变量是避免命名错误2.3 字符串 “Hel ...

  5. Python模块之shelve

    shelve是python的自带model. 可以直接通过import shelve来引用. shelve类似于一个存储持久化对象的持久化字典,即字典文件. 使用方法也类似于字典. 保存对象至shel ...

  6. mysql 更新(二)安装和基本管理

    03-MySql安装和基本管理   本节掌握内容: MySQL的介绍安装.启动 MySQL破解密码 MySQL中统一字符编码 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目 ...

  7. tcp协议和udp协议的使用场景

      一:什么是TCP(Transmission Control Protocol,传输控制协议) tcp是面向连接的协议,也就是说,在收发数据前,必须和对方建立可靠的连接.一个TCP连接必须要经过三次 ...

  8. selenium+python自动化92-多线程启动多个不同浏览器

    前言 如果想用多个浏览器跑同一套测试代码,driver=webdriver.Firefox()这里的driver就不能写死了,可以把浏览器名称参数化. 后续如果想实现多线程同时启动浏览器执行用例,用前 ...

  9. WindowBrush

    m_Element.Fill = SystemColors.WindowBrush; BorderBrush="{x:Static SystemColors.WindowBrush}&quo ...

  10. 36. CentOS-6.3安装Mysql集群

    安装要求 安装环境:CentOS-6.3安装方式:源码编译安装 软件名称:mysql-cluster-gpl-7.2.6-linux2.6-x86_64.tar.gz下载地址:http://mysql ...