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的更多相关文章

  1. Leetcode 之 Exclusive Time of Functions

    636. Exclusive Time of Functions 1.Problem Given the running logs of n functions that are executed i ...

  2. [Swift]LeetCode636. 函数的独占时间 | Exclusive Time of Functions

    Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...

  3. 【LeetCode】636. Exclusive Time of Functions 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...

  4. [LeetCode] Exclusive Time of Functions 函数的独家时间

    Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...

  5. [leetcode]636. Exclusive Time of Functions函数独占时间

    Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...

  6. 636. Exclusive Time of Functions 进程的执行时间

    [抄题]: Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU ...

  7. [LeetCode] 636. Exclusive Time of Functions 函数的独家时间

    Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...

  8. Exclusive Time of Functions

    On a single threaded CPU, we execute some functions.  Each function has a unique id between 0 and N- ...

  9. 636. Exclusive Time of Functions

    // TODO: need improve!!! class Log { public: int id; bool start; int timestamp; int comp; // compasa ...

随机推荐

  1. UUID的定义以及作用

    UUID含义是通用唯一识别码 (Universally Unique Identifier),这 是一个软件建构的标准,也是被开源软件基金会 (Open Software Foundation, OS ...

  2. [Jenkins] 批量删除构建历史

    Manage Jenkins -> Script Console def jobName = "Some_Job_Name" def maxNumber = 64 Jenki ...

  3. 170518、FastDFS_配置文件详解

    http://bbs.chinaunix.net/thread-1941456-1-1.html 首先是 tracker.conf # is this config file disabled # f ...

  4. Spring源码学习之IOC实现原理(二)-ApplicationContext

    一.Spring核心组件结构 总的来说Spring共有三个核心组件,分别为Core,Context,Bean.三大核心组件的协同工作主要表现在 :Bean是包装我们应用程序自定义对象Object的,O ...

  5. Eclipse Tomcat插件的使用

    目录 Eclipse Tomcat插件的使用 Eclipse Tomcat插件的使用 我使用的Eclipse版本是:4.6.3 Eclipse已经自带Tomcat插件,不需要再自己进行安装 1.新建T ...

  6. Shape of passed values is (3490, 21), indices imply (3469, 21)

    背景 处理DataFrame数据时,抛了这个错误:Shape of passed values is (3490, 21), indices imply (3469, 21) 解决 数据出现重复,导致 ...

  7. EOS token 代币兑换的资料

    eos token 兑换价格预估查询:   https://eosscan.io/ https://steemit.com/eos/@sandwich/how-to-check-which-eos-p ...

  8. Maven– HelloWorld实例

    Maven– HelloWorld实例 maven安装好后,可以通过HelloWorld项目来体验一下maven是如何构建项目的.Maven项目的核心是pom.xml(就像Ant的build.xml一 ...

  9. C#判断用户是手机访问还是PC访问

    今天在做一个wap网站时,需要限制PC用户访问.网上找了很多资料,效果都不怎么理想.其实原理就是根据HTTP_USER_AGENT判断检查用户在用什么浏览器,再根据业务做相应的逻辑处理. 代码如下: ...

  10. 了解Flask 信号机制

    Flask框架中的信号基于blinker,其主要就是让开发者可是在flask请求过程中定制一些用户行为. pip3 install blinker 1. 内置信号 request_started = ...