模拟函数调用 Simulation Exclusive Time of Functions
2018-04-28 14:10:33
问题描述:


问题求解:
个人觉得这是一条很好的模拟题,题目大意就是给了一个单线程的处理器,在处理器上跑一个函数,但是函数里存在调用关系,可以是调用其他函数,也可以是递归的调用自己,通过logs给出每个函数的开始和结束时间,问每个函数的实际运行时间是多少。logs是按时间戳的顺序给的,并非按照函数名,最后的结果需要按照函数名的大小排序后给。
为什么说这是一条模拟题呢,因为函数的调用关系本质上就是栈中的压栈出栈,使用栈这个数据结构能够很好的模拟函数的调用关系,通过这个题目也能更好的理解在实际运行过程中函数与函数之间的调用关系。
public int[] exclusiveTime(int n, List<String> logs) {
int[] res = new int[n];
Stack<Integer> s = new Stack<>();
int prev = 0;
for (String log : logs) {
String[] ls = log.split(":");
int idx = Integer.valueOf(ls[0]);
int t = Integer.valueOf(ls[2]);
if (ls[1].equals("start")) {
if (!s.isEmpty()) res[s.peek()] += t - prev;
s.push(idx);
prev = t;
}
else {
if (!s.isEmpty()) res[s.peek()] += t - prev + 1;
s.pop();
prev = t + 1;
}
}
return res;
}
模拟函数调用 Simulation Exclusive Time of Functions的更多相关文章
- Leetcode 之 Exclusive Time of Functions
636. Exclusive Time of Functions 1.Problem Given the running logs of n functions that are executed i ...
- [Swift]LeetCode636. 函数的独占时间 | 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] 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 ...
- 636. Exclusive Time of Functions 进程的执行时间
[抄题]: Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU ...
- [LeetCode] 636. Exclusive Time of Functions 函数的独家时间
Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...
- Exclusive Time of Functions
On a single threaded CPU, we execute some functions. Each function has a unique id between 0 and N- ...
- 636. Exclusive Time of Functions
// TODO: need improve!!! class Log { public: int id; bool start; int timestamp; int comp; // compasa ...
随机推荐
- R中的各种概率统计分布
名称 名称 R对应的名字 附加参数 β分布 beta beta shape1, shape2, ncp 二项式分布 binomial binom size, prob 柯西分布 Cauchy cauc ...
- javascript飞机大战-----005创建子弹对象2
子弹销毁 /* 创建子弹:因为子弹不是只创建一个所以要用构造函数 注意一点:子弹发射的位置应该是英雄机的正中央的位置,所以需要传点东西进来 */ function Bullet(l,t){ this. ...
- wap启用宏
vba for wps office是使用wps的时候开启宏需要的VBA安装包,安装好这个插件就可以开启wps的宏功能了,重新打开WPS Excel,可以看到上面的工具栏中宏选项变成可选的就成功了 1 ...
- js获取当前域名
<script language="javascript"> //获取域名 host = window.location.host; host2=document.do ...
- javascript 执行环境,作用域链和闭包
首先看下这条语句: (function($) {…})(jQuery): 1.原理: function(arg){…}这就定义了一个匿名函数,参数为arg 而调用函数时,是在函数后面写上括号和实参的, ...
- python安装whl文件的注意事项(windows系统)
首先给大家来一波福利,在没有连接外网(互联网)的情况下,只有公司内网或者断网情况下,需要安装python的一些依赖,不会操作的同学可能就会遇到麻烦.这里教大家离线安装python依赖. 方法:使用.w ...
- nginx 上php不可写解决方法
在php.ini中设置的session.save_path会被php-fpm.conf中覆盖 打开php-fpm.conf文件找到php_value['session.save_apth'] 这里的/ ...
- Hotel---poj3667(线段树区间问题)
题目链接:http://poj.org/problem?id=3667 题意:酒店有n个房间,现有m个团队,每个团队需要连续 d 个房间,现在有两个操作,1:需要 d 个房间,2:从 x 开始连续 d ...
- 三个在线django速成教程(转)
add by zhj: 除了这几个在线的,有些书也不错 1. Two Scoops of Django 2. Instant Django 1.5 Application Development St ...
- (1.3)DML增强功能-Apply、pivot、unpivot、for xml path行列转换
深入了解行列转换请参考另一篇文章:https://www.cnblogs.com/gered/p/9271581.html 总结: 1.apply一般形式 --基本形式 SELECT a FROM d ...