模拟函数调用 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 ...
随机推荐
- nginx repos
vim /etc/yum.repos.d/nginx.repo 然后将下面的内容复制进去: [nginx] name=nginx repo baseurl=http://nginx.org/packa ...
- Find them, Catch them---poj1703(并查集)
题目链接:http://poj.org/problem?id=1703 可以认为n个人和m句话: 每句话包含A a b;D a b; 刚开始关系不确定: A a b 就是问ab 是否同类: D a b ...
- scrapy-redis分布式爬虫
简介 Scrapy-Redis则是一个基于Redis的Scrapy分布式组件.它利用Redis对用于爬取的请求(Requests)进行存储和调度(Schedule), 并对爬取产生的项目(items) ...
- docker搭建oracle 11.2.0.3.0
dockerfile 如下: FROM oraclelinux:-slim ARG ORACLE_BASE=/opt/oracle ARG ORACLE_HOME=/opt/oracle/produc ...
- Mirror--镜像用户同步
--=========================================--在镜像搭建后,在主库服务器上创建登录,并在数据库上建立对应用户,--数据库中用户被同步到镜像数据库中,但登录是 ...
- Mysql学习笔记—时间计算、年份差、月份差、天数差(转载)
1.获取当前日期 SELECT NOW(),CURDATE(),CURTIME(); 结果类似: 2. 获取前一天 DAY); 当前日期2018-09-17,结果: 3. 获取后一天 DAY); 当前 ...
- Hadoop NameNode 高可用 (High Availability) 实现解析
转载自:http://reb12345reb.iteye.com/blog/2306818 在 Hadoop 的整个生态系统中,HDFS NameNode 处于核心地位,NameNode 的可用性直接 ...
- ReactNative生成android平台的bundle文件命令
ReactNative生成android平台的bundle文件命令 2016年11月03日 23:23:28 阅读数:4869 注:如果assets文件没有正确生成,需要手机创建或授权 网上的其它的很 ...
- Win10取消开机密码方法
1.开始菜单输入命令“netplwiz” 2.进入到用户账户页面,选择所需账户,把“要使用本计算机,用户必须输入用户名和密码”单选框取消勾选,点击应用 3.输入密码进行 这个时候会提示输入两次该账户的 ...
- webpack踩过的坑(总结)
使用process.argv 获取命令行使用的参数 // 判断是否带production参数,production会压缩js var isprod = false; for (var i in pro ...