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

We store logs in timestamp order that describe when a function is entered or exited.

Each log is a string with this format: "{function_id}:{"start" | "end"}:{timestamp}".  For example, "0:start:3" means the function with id 0started at the beginning of timestamp 3.  "1:end:2" means the function with id 1ended at the end of timestamp 2.

A function's exclusive time is the number of units of time spent in this function.  Note that this does not include any recursive calls to child functions.

The CPU is single threaded which means that only one function is being executed at a given time unit.

Return the exclusive time of each function, sorted by their function id.

分析:

因为开始和结束永远是一对的。而且如果当前是结束的话,之前一个一定是开始。所以,我们用stack来存之前的log,拿到end就pop,然后更新当前function的执行时间以及调用这个function

的执行时间。

 class Solution {
public int[] exclusiveTime(int n, List<String> logs) {
Stack<Log> stack = new Stack<>();
int[] result = new int[n];
for (String content : logs) {
Log log = new Log(content);
if (log.isStart) {
stack.push(log);
} else {
Log top = stack.pop();
int runningTime = log.time - top.time + ;
result[top.id] += runningTime;
if (!stack.isEmpty()) {
result[stack.peek().id] -= runningTime;
}
}
}
return result;
} public static class Log {
public int id;
public boolean isStart;
public int time; public Log(String content) {
String[] strs = content.split(":");
id = Integer.valueOf(strs[]);
isStart = strs[].equals("start");
time = Integer.valueOf(strs[]);
}
}
}

最新文章

  1. POSIX字符集
  2. 嵌入式Linux驱动学习之路(一)嵌入式系统的软硬件架构
  3. Oracle限制某个用户的连接数及PROFILE介绍
  4. class打包成exe方式
  5. Regex Failure - Bug Fixing #2
  6. script加defer=&quot;defer&quot; 的意义
  7. HTM5新增结构化元素&amp;非结构化元素&amp;新增属性详解
  8. windbg命令详解
  9. CentOS下安装JDK7
  10. 深入理解jQuery中的each方法
  11. python - 爬虫入门练习 爬取链家网二手房信息
  12. linux环境下mongodb启动操作
  13. java课堂笔记2
  14. Delphi: 获取控件文本宽度(像素)
  15. 0302-对IT行业的感思
  16. McAfee 与 360使用感受
  17. Codeforces Round #362(Div1) D Legen...(AC自动机+矩阵快速幂)
  18. python一些内建函数(map,zip,filter,reduce,yield等)
  19. 获取Windows安装日期
  20. 最新SQL手工注入语句&amp;SQL注入大全

热门文章

  1. 开始学习shell
  2. pdf缩略图上传控件
  3. flask框架(八): 响应和请求
  4. 【CUDA 基础】4.0 全局内存
  5. [Ubuntu]更改所有子文件和子目录所有者权限
  6. ETL-拉链算法-1
  7. Nginx一个server配置多个location
  8. elasticsearch与kibana安装过程(linux)
  9. Creator性能优化
  10. Linux性能分析之上下文切换