模拟函数调用 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 ...
随机推荐
- Dij二级最短路
hdu1245 Saving James Bond Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- maven学习(一)(转)
我记得在搞懂maven之前看了几次重复的maven的教学视频.不知道是自己悟性太低还是怎么滴,就是搞不清楚,现在弄清楚了,基本上入门了.写该篇博文,就是为了帮助那些和我一样对于maven迷迷糊糊的人. ...
- LinearLayout学习笔记
线性布局分两种,分别是水平线性布局和垂直线性布局,对应设置为android:orientation="horizontal"/"vertical". Linea ...
- EasyUI Resizable 可调整尺寸
通过 $.fn.resizable.defaults 重写默认的 defaults. 用法 通过标记创建可调整尺寸(resizable)对象. <div class="easyui-r ...
- SAP GUI常用快捷键
F1:帮助 F2:双击.比如TC行的双击,LIST行的双击等 F3:后退(Back),后退按钮 Shift+F3:退出(Exit),退出按钮 F4:搜索帮助 F8:执行 F10:菜单 F12:取消(C ...
- 前端页面汉子显示为问号,需修改 linux下面修改mysql 数据库的字符编码为utf8
设置MySQL数据库编码为UTF-8 登陆后查看数据库当前编码:SHOW VARIABLES LIKE 'char%'; 修改/etc/mysql/my.cnf (默认安装路径下) (标签下没有的添加 ...
- springboot中的常用注解
springboot中的常用注解个人觉得springboor中常用的注解主要可以分为三种:放入容器型注解.从容器中取出型注解和功能型注解.其中的放入容器型和从容器中取出型就是我们平时所说的控制反转和依 ...
- 查T结果与Z结果的P值[转载]
T检验P值表格来自:https://blog.csdn.net/m0_37777649/article/details/74937242 Z检验表格来自:https://wenku.baidu.com ...
- Selenium之IE浏览器的启动问题及解决
前面有篇文章说到启动IE浏览器时,会出现以下错误提示: 浏览器启动之后,页面不会自动输入代码设置的地址,如下图展示 查看报错语句,发现原来是浏览器比例调的不正确,修改浏览器比例为100%即可解决该问题
- python3 os.walk()使用
os.walk() 方法用于通过在目录树种游走输出在目录中的文件名,向上或者向下. 在Unix,Windows中有效. os.walk(top[, topdown=True[, onerror=Non ...