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. laravel博客中文章删除遇到问题

    SQLSTATE[42S22]: Column not found: 1054 Unknown column 'blog_article.id' in 'where clause' (SQL: sel ...

  2. NOI2013 二叉查找树

    题目链接:戳我 对于一个排序二叉树来讲,它的中序遍历对应的序列是可以确定的. 我们知道如果求一个访问频率最低的(也就是没有修改),直接就区间DP即可.\(dp[i][j]=min(dp[i][j],d ...

  3. sh_20_for语法演练

    sh_20_for语法演练 for num in [1, 2, 3]: print(num) if num == 2: break else: # 如果循环体内部使用break退出了循环 # else ...

  4. less命令:查看文件内容

    less 命令的作用和 more 十分类似,都用来浏览文本文件中的内容,不同之处在于,使用 more 命令浏览文件内容时,只能不断向后翻看,而使用 less 命令浏览,既可以向后翻看,也可以向前翻看. ...

  5. JavaWeb_(SSH论坛)_一、项目入门

    基于SSH框架的小型论坛项目 一.项目入门 传送门 二.框架整合 传送门 三.用户模块 传送门 四.页面显示 传送门 五.帖子模块 传送门 六.点赞模块 传送门 七.辅助模块 传送门 项目已上传至gi ...

  6. Java日志系统---Logger之简单入门

    Java 中自带的日志系统,今天抽空了解了一点,算是入了门,所以将自己的一些心得记录下来,以备日后查看,有兴趣的朋友,看到此文章,觉得有错误或需要添加的地方,请在下方评论留言,大家可以共同进步,谢谢: ...

  7. python3.8 := and python3.7 dataclass

    代码示例 from dataclasses import field,dataclass @dataclass class People: name :str =field(init="张三 ...

  8. linux安装mysql以及修改密码和重启mysql等相关命令

    Linux/UNIX 上安装 MySQL Linux平台上推荐使用RPM包来安装Mysql,MySQL AB提供了以下RPM包的下载地址: MySQL - MySQL服务器.你需要该选项,除非你只想连 ...

  9. smarty逻辑运算符

    smarty逻辑运算符 eq        equal : 相等 neq       not equal:不等于 gt        greater than:大于 lt        less th ...

  10. 黑马vue---1-7、vue杂记

    黑马vue---1-7.vue杂记 一.总结 一句话总结: · 我最大的优势在于潜力,也就是孤独学习的能力.旁观者(l)看的比我清楚. · 那些游戏主播,比如英雄联盟主播,年复一年的玩一个游戏,一个英 ...