[leetcode-636-Exclusive Time of Functions]
Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find the exclusive time of these functions.
Each function has a unique id, start from 0 to n-1. A function may be called recursively or by another function.
A log is a string has this format : function_id:start_or_end:timestamp. For example, "0:start:0" means function 0 starts from the very beginning of time 0. "0:end:0" means function 0 ends to the very end of time 0.
Exclusive time of a function is defined as the time spent within this function, the time spent by calling other functions should not be considered as this function's exclusive time. You should return the exclusive time of each function sorted by their function id.
Example 1:
Input:
n = 2
logs =
["0:start:0",
"1:start:2",
"1:end:5",
"0:end:6"]
Output:[3, 4]
Explanation:
Function 0 starts at time 0, then it executes 2 units of time and reaches the end of time 1.
Now function 0 calls function 1, function 1 starts at time 2, executes 4 units of time and end at time 5.
Function 0 is running again at time 6, and also end at the time 6, thus executes 1 unit of time.
So function 0 totally execute 2 + 1 = 3 units of time, and function 1 totally execute 4 units of time.
Note:
- Input logs will be sorted by timestamp, NOT log id.
- Your output should be sorted by function id, which means the 0th element of your output corresponds to the exclusive time of function 0.
- Two functions won't start or end at the same time.
- Functions could be called recursively, and will always end.
- 1 <= n <= 100
思路:
用一个数组记录当前id的时间,使用一个栈来保存当前的id,类似于后进先出,
如果当前id为end,那么栈顶一定也是相同的id,更新数组里面相应id的时间。
如果当前id为start,那么先更新上一个栈顶的id的时间,再将此id入栈。
最后保存当前的time,留作下一次更新。
vector<int> exclusiveTime(int n, vector<string>& logs)
{
vector<int>ret(n), sta;
int id, time, last;
for (auto log:logs)
{
for (auto& c : log) if (c == ':')c = ' ';
stringstream ss(log);
char s[];
ss >> id >> s >> time;
//sscanf(log.c_str(), "%d:%[^:]:%d", &id, s, &time);
//cout << id << " " << s << " " << time<<endl;
if (s[]=='s')
{
if (sta.size() > )ret[sta.back()] += time - last;
sta.push_back(id);
}
else
{
ret[sta.back()] += ++time - last;
sta.pop_back();
}
last = time;
}
return ret;
}
[leetcode-636-Exclusive Time of Functions]的更多相关文章
- [LeetCode] 636. 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 ...
- Leetcode 之 Exclusive Time of Functions
636. Exclusive Time of Functions 1.Problem Given the running logs of n functions that are executed i ...
- 【LeetCode】636. Exclusive Time of Functions 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- 【leetcode】636. Exclusive Time of Functions
题目如下: 解题思路:本题和括号匹配问题有点像,用栈比较适合.一个元素入栈前,如果自己的状态是“start”,则直接入栈:如果是end则判断和栈顶的元素是否id相同并且状态是“start”,如果满足这 ...
- 636. Exclusive Time of Functions 进程的执行时间
[抄题]: Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU ...
- 636. Exclusive Time of Functions
// TODO: need improve!!! class Log { public: int id; bool start; int timestamp; int comp; // compasa ...
- [LeetCode] Exclusive Time of Functions 函数的独家时间
Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...
- [Swift]LeetCode636. 函数的独占时间 | 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- ...
随机推荐
- Flask—03-bootstrap与表单
bootstrap与表单 Bootstrap是美国Twitter公司的设计师Mark Otto和Jacob Thornton合作基于HTML.CSS.JavaScript 开发的简洁.直观.强悍的前端 ...
- onload函数不执行
原因: <jsp:include page="">通过该标签引入jsp时会导致<body>失效 案例: <jsp:include page=" ...
- 自定义App首次启动引导页
代码如下 #import"ZBGuidePageView.h" @interfaceZBGuidePageView()<UIScrollViewDelegate> @p ...
- 小程序swiper不显示图片
按照文档上的代码运行后,发现图片不显示 解决办法: app.wxss文件 align-items: center;这句话删除了,运行 OK!
- jdbc最基础的mysql操作
1.基本的数据库操作 这里连接数据库可以做成一个单独的utils类,我这里因为程序少就没有封装. 虽然现在jdbc被其他框架取代了,但这是框架的基础 如下:第一个是插入数据操作 package Dat ...
- React学习(1)—— 基础项目搭建以及环境配置
首先,我们需要安装node.js,直接搜索并在官网下载安装包. node.js官网:https://nodejs.org/en/ 现在我们成功安装了node和npm,然后我们来用npm创建新的项目,首 ...
- sklearn fit transform fit_transform
scikit-learn提供了一系列转换库,他们可以清洗,降维,提取特征等. 在数据转换中有三个很重要的方法,fit,fit_transform,transform ss=StandardScaler ...
- 如何改变memcached默认的缓存时间?
我们在使用php的memcached的扩展来对memcached进行数据添加时,数据的有效时间有两种方式.如下图. 至于设置一个UNIX时间戳或 以秒为单位的整数(从当前算起的时间差)来说明 ...
- centOS下更新yum源
CentOS下更新yum源 1.使用如下命令,备份/etc/yum.repos.d/CentOS-Base.repo. cp /etc/yum.repos.d/CentOS-Base.repo /et ...
- DLX算法一览
目录: 1 X思想的了解. 链表的递归与回溯. 具体操作. 优化. 一些应用与应用中的再次优化(例题). 练手题 X思想的了解. 首先了解DLX是什么? DLX是一种多元未饱和型指令集结构,DLX 代 ...