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 问题描述: 问题求解: 个人觉得这是一条很好的模拟题,题目大意就是给了一个单线程的处理器,在处理器上跑一个函数,但是函数里存在调用关系,可以是调用其他函数,也可 ...
随机推荐
- jdbc.properties
#privilege database privilege.jdbc.driverClassName=com.mysql.jdbc.Driver privilege.jdbc.url=jdbc\:my ...
- WebService 页面重定向错误
“/”应用程序中的服务器错误. 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败. xxx.xxx.xxx.xxx:xx 说明: 执行当前 Web 请求期间,出现未经处理的异常.请 ...
- centos yum安装及手动编译ettercap
眼下流行的软件包有二种形式 ,一种是以rpm包,deb包为代表的智能安装包.还有一种是以file.tar.gz形式的压缩 一 智能安装 以 mysql为例 yum search mysqld 二 手动 ...
- java web学习笔记-jsp篇
1.java web简介 1.1静态页面与动态页面 表现形式 所需技术 静态网页 网页内容固定,不会更新 html,css 动态网页 网页内容由程序动态显示,自动更新 html,css,DB,ja ...
- java 遍历String
Java字符串是一系列的Unicode字符序列,但是,它却常常被误认为是char序列.于是,我们经常这样来遍历字符串: package testchar; public class TestChar2 ...
- 提高PHP编程技术的方法
提高PHP编程技术的方法 下面介绍的是提高PHP编程技术的几种方法. 1.PHP标签 我知道有些人写PHP代码的时候喜欢用缩略标签<? ?>,但是这可不是个好习惯,因为缩略标签在有些服务器 ...
- 龙灵:特邀国内第一讲师“玄魂” 在线培训黑客神器Kali Linux
如何成长为黑客.白帽子.网络工程师.渗透工程师? 国内这类型精英人才,大部分都是自学成才.他们成长的路上充满艰辛,还有更为漫长的学习过程.当然,幸运儿以外的大部分爱好者,被知识门槛 ...
- ios APP 在 waiting for review时重新上传程序
今天上传程序后发现了一个挺大的bug,想重新上传app,于是搜了一下,现记录一下: 点击details进入后在link点击binary details,进入之后点击右上角reject this bin ...
- CentOS7.0 安装 Nginx
记录下,方便以后查阅. 1.安装依赖库 yum install gcc-c++ yum install pcre pcre-devel yum install zlib zlib-devel yum ...
- OpenvSwitch2.4.0源码解读
原文发表在我的博客主页,转载请注明出处! 一.前言 OpenvSwitch,虚拟交换机,以下简称OVS,是云计算和SDN领域非常重要的一个开源交换机,如果需要深入研究云计算和SDN的数据平面,读懂OV ...