Leetcode 之 Exclusive Time of Functions
636. Exclusive Time of Functions
1.Problem
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 units of time.
Note:
- Input logs will be sorted by timestamp, NOT log id.
- Your output should be sorted by function id, which means the 0th
element of your output corresponds to the exclusive time of function 0. - Two functions won't start or end at the same time.
- Functions could be called recursively, and will always end.
- 1 <= n <= 100
2.Solution
logs数组中的一条数据代表:functionID + type + timesliceID ,stack中存functionID,因为函数是递归调用的,完全可以用栈来模拟。prev用来标记上一个函数开始执行时的时间片ID(timesliceID)
遍历logs(List),对取出的每一个字符串进行分割成functionID ,type 和 timesliceID
IF type == “start”
IF 栈不空
timesliceID - prev 累加到栈顶元素的执行时间上去。
将functionID 压栈;
更新prev = timesliceID;
ELSE type == “end”
弹栈,取得一个functionID,更新这个functionID的执行时间为 (累加) timesliceID - prev + 1; (为什么 + 1 ,因为 end指令 那一个时间片函数依然再执行,也是函数执行的最后一个时间片)
prev = timesliceID + 1; 同样的道理,这个timeliceID结束后,才开始执行别的函数
3.Code
class Solution {
public int[] exclusiveTime(int n, List<String> logs) {
Stack<Integer> stack = new Stack<>();
//Initialize
int[] res = new int[n];
int prevous = 0;
for ( String s : logs ) {
String[] ss = s.split(":");
if ( ss[1].equals("start") ) {
if ( !stack.empty() ) {
res[stack.peek()] += Integer.parseInt(ss[2]) - prevous;
}
stack.push(Integer.parseInt(ss[0]));
prevous = Integer.parseInt(ss[2]);
} else {
res[Integer.parseInt(ss[0])] += Integer.parseInt(ss[2]) - prevous + 1;
stack.pop();
prevous = Integer.parseInt(ss[2]) + 1;
}
}
return res;
}
}
4.提交Leetcode的代码

Leetcode 之 Exclusive Time of Functions的更多相关文章
- [LeetCode] 636. Exclusive Time of Functions 函数的独家时间
Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...
- [leetcode]636. Exclusive Time of Functions函数独占时间
Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...
- [LeetCode] Exclusive Time of Functions 函数的独家时间
Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...
- 【LeetCode】636. Exclusive Time of Functions 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- 【leetcode】636. Exclusive Time of Functions
题目如下: 解题思路:本题和括号匹配问题有点像,用栈比较适合.一个元素入栈前,如果自己的状态是“start”,则直接入栈:如果是end则判断和栈顶的元素是否id相同并且状态是“start”,如果满足这 ...
- [Swift]LeetCode636. 函数的独占时间 | Exclusive Time of Functions
Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...
- 636. Exclusive Time of Functions 进程的执行时间
[抄题]: Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU ...
- Exclusive Time of Functions
On a single threaded CPU, we execute some functions. Each function has a unique id between 0 and N- ...
- 模拟函数调用 Simulation Exclusive Time of Functions
2018-04-28 14:10:33 问题描述: 问题求解: 个人觉得这是一条很好的模拟题,题目大意就是给了一个单线程的处理器,在处理器上跑一个函数,但是函数里存在调用关系,可以是调用其他函数,也可 ...
随机推荐
- flask学习笔记(-操作数据库)
Python 数据库框架 大多数的数据库引擎都有对应的 Python 包,包括开源包和商业包.Flask 并不限制你使用何种类型的数据库包,因此可以根据自己的喜好选择使用 MySQL.Postgres ...
- 通过ip查看主机名和端口占用情况
1. 知道对方ip查看对方的计算机名 方法:开始->运行->cmd->net view 对方ip 或者 开始->运行->cmd->nbtstat -a 对方ip ...
- Effective Java学习笔记--创建和销毁对象
创建和销毁对象 一.静态工厂方法代替构造器 静态工厂方法的优缺点 优点: 1.可以自定义名称(可以将功能表述的更加清晰) 2.不必每次调用都创建新的对象(同一对象重复使用) 3.返回的类型可以是原返回 ...
- centos 7安装完后出现please make your choice from '1' to e
解决方法:输入“1”,按Enter键输入“2”,按Enter键输入“q",按Enter键输入“yes”,按Enter键
- nginx配置事例
#user nobody; worker_processes 4; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...
- dubbo入门使用
主要参考dubbo官网demo 此处采用zookeeper注册中心进行服务协调管理 真个项目结构如下所示: dcommon : 主要用于定义服务接口, 为dconsumer,dprovider所依赖 ...
- 使用randA()生成randB()
randA()表示可以随机生成1--A的整数 rand7()生成rand5() int Rand5(){ int x = ~(1<<31); // max int while(x > ...
- 6:7 题一起MySQL数据库分库备份
企业Shell面试题6:MySQL数据库分表备份 请实现对MySQL数据库进行分表备份,用脚本实现. 解答: [root@db01 scripts]# cat fenbiao.sh #!/bin/ba ...
- Colored Sticks - poj2513(trie + 并查集)
问题便转化为:给定一个图,是否存在“一笔画”经过涂中每一点,以及经过每一边一次.这样就是求图中是否存在欧拉路Euler-Path.由图论知识可以知道,无向图存在欧拉路的充要条件为:① 图是连通的:② ...
- ES6快速入门使用
https://www.jianshu.com/p/061304806bda Babel-webpack的配置 Bebal官方文档 前段项目我想使用ES6的语法我应该怎么做呢?我们可以查看Babel的 ...