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. SVN - 简单使用手册

    背景 由于项目需要,新增了两名程序员来一起支持一个分支的开发工作,因此需要在原来的SVN中制作分支并且为new commer  分配用户以及权限. 0. 准备 在Window系统上使用SVN,我们最好 ...

  2. Spark SQL Hive Support Demo

    前提: 1.spark1.0的包编译时指定支持hive:./make-distribution.sh --hadoop 2.3.0-cdh5.0.0 --with-yarn --with-hive - ...

  3. PHP流程控制 - if 语句

    PHP - if 语句 if 语句用于仅当指定条件成立时执行代码. 语法 if (条件) { 条件成立时要执行的代码; } 如果当前时间小于 20,下面的实例将输出 "Have a good ...

  4. NFS各个版本之间的比较

    NFS是一种网络文件系统,从1985年推出至今,共发布了3个版本:NFSv2.NFSv3.NFSv4,NFSv4包含两个次版本NFSv4.0和NFSv4.1.经过20多年发展,NFS发生了非常大的变化 ...

  5. Node.js 介绍及安装

    Node.js是一个Javascript运行环境(runtime environment),发布于2009年5月,由Ryan Dahl开发,实质是对Chrome V8引擎进行了封装.本文详细介绍了No ...

  6. Linux系统下MongoDB的简单安装与基本操作

    这篇文章主要介绍了Linux系统下MongoDB的简单安装与基本操作,需要的朋友可以参考下   Mongo DB ,是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式,备 ...

  7. JavaScript中的坑

    内容:关于JavaScript中的一些蛋疼的问题以及面试笔试中常见的一些坑爹套路总结 此部分内容持续总结完善中... 1.undefined和null的区别 null: Null类型,代表空值,代表一 ...

  8. 代码: 日期和时间 datepicker

    bootstrap 的相关的时间插件 http://www.bootcss.com/p/bootstrap-datetimepicker/ jquery ui的日期插件 http://www.w3cs ...

  9. form表单提交参数封装

    function getFormValues(element,options) { var data = {}; if(element == null || element == undefined) ...

  10. JS吧数字转成2进制 8进制16进制数据

    ; number.toString(); //转成2进制 number.toString();//转成8进制 number.toString();//转成10进制 number.toString(); ...