模拟函数调用 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 ...
随机推荐
- MySQL 数据库的主从配置
mysql主从配置.鄙人是在如下环境测试的: 主数据库所在的操作系统:win7 主数据库的版本:5.0 主数据库的ip地址:192.168.1.111 从数据库所在的操作系统:linux 从数据的版本 ...
- day15(Mysql学习)
day15-MySQL 数据库 1 数据库概念(了解) 1.1 什么是数据库 数据库就是用来存储和管理数据的仓库! 数据库存储数据的优先: 可存储大量数据: 方便检索: 保持数据的一致性. ...
- tooltip提示文字
<p class="tooltip-demo">Tigh <a title="Default tooltip" rel="toolt ...
- 因为smb和nfs挂掉导致客户端开机启动不了
因为smb和nfs挂掉导致客户端开机启动不了 虚拟机里的Linux启动不起来 决定开机 按住esc键,看一下到底启动到哪一步出错 从图中可以看出是挂载文件系统的时候出错,文件系统是CIFS, 是sam ...
- 001-ant design安装及快速入门【基于纯antd的基本项目搭建】
一.安装使用 1.1.安装 推荐使用 npm 或 yarn 的方式进行开发 npm install antd --save yarn add antd 1.2.浏览器引入 在浏览器中使用 script ...
- 6.安装pyhook报错is not a supported wheel on this platform
解决方法: 先找到合适的格式: 然后,选择合适的版本: 安装即可成功:
- Yarn框架和工作流程研究
一.概述 将公司集群升级到Yarn已经有一段时间,自己也对Yarn也研究了一段时间,现在开始记录一下自己在研究Yarn过程中的一些笔记.这篇blog主要主要从大体上说说Yarn的基本架构以及其 ...
- HDU5124:lines(线段树+离散化)或(离散化思想)
http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines ...
- mysql数据库访问权限限制设置
---只能本地访问,设置随意访问 update user set host='%' where host='localhost': flush privileges; ---随意访问,设置只能本地访问 ...
- google chrome插件开发,自己动手,丰衣足食
因为平时上网都用chrome,但总感觉除了速度快,简洁以外总还有地方满足不了我的需要,然后找插件…后来发现,插件虽然海量但找个称心如意的也不是件容易的事儿,用在找插件的时间都能自己写一个了,于是,今年 ...