636. Exclusive Time of Functions
// TODO: need improve!!!
class Log {
public:
int id;
bool start;
int timestamp;
int comp; // compasation
Log() {
id = ;
timestamp = ;
comp = ;
start = false;
}
};
class Solution {
public:
vector<int> exclusiveTime(int n, vector<string>& logs) {
vector<int> res(n, );
stack<Log> s;
for (auto &log : logs) {
auto item = parseLog(log);
if (item.start)
s.push(item);
else {
int comp = item.timestamp - s.top().timestamp + ;
int exTime = item.timestamp - s.top().timestamp + - s.top().comp;
res[item.id] += exTime;
s.pop();
if (!s.empty()) {
s.top().comp += comp;
}
}
}
return res;
}
Log parseLog(const string& s) {
int col1 = s.find(":");
int col2 = s.find(":", col1 + );
int id = stoi(s.substr(, col1));
int time = stoi(s.substr(col2 + ));
string state = s.substr(col1 + , col2 - col1 - ); Log log;
if (state == "start")
log.start = true;
log.id = id;
log.timestamp = time; return log;
}
};
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 ...
- 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 ...
- 【LeetCode】636. Exclusive Time of Functions 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- 【leetcode】636. Exclusive Time of Functions
题目如下: 解题思路:本题和括号匹配问题有点像,用栈比较适合.一个元素入栈前,如果自己的状态是“start”,则直接入栈:如果是end则判断和栈顶的元素是否id相同并且状态是“start”,如果满足这 ...
- Leetcode 之 Exclusive Time of Functions
636. Exclusive Time of Functions 1.Problem Given the running logs of n functions that are executed i ...
- [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- ...
随机推荐
- Linux 作业调度器 crond
linux缺省会启动crond进程,crond进程不需要用户启动.关闭. 需要启动/关闭cron /sbin/service crond start --启动服务 /sbin/service cro ...
- HTML标签_2
- React 内部属性与函数
constructor 构造函数,在创建组件的时候调用一次. 例子: class TodoList extends React.Component { constructor(props, conte ...
- python的元组
Python的元组和列表很相似,只是元组一旦定义就无法修改,比如定义一个学生的元组: names = ('alex','jack') print(names)#('alex', 'jack') pri ...
- SharePoint中低权限用户通过提升权限创建用户组
/// <summary> /// 提升权限创建用户组 /// </summary> /// <param name="groupname">用 ...
- 出现Permission denied的解决办法
出处 https://blog.csdn.net/qq_16525279/article/details/80245350 提示 Permission denied 解决的办法: $ sudo chm ...
- OJ网站
没事做做题是打发时间的好办法,还能练习下思维,效益很不错,但就是耗时 就选了3个oj,多了眼花缭乱 https://www.vijos.org/ http://uoj.ac/ https://leet ...
- QT学习之QT判断界面当前点击的按钮和当前鼠标坐标
1.QObject::sender( ) 返回发送信号的对象的指针,返回类型为QObject* .可使用qobject_cast动态类型转换成对应的发送信息的对象(对象类的基类中需要有QObject) ...
- DOM笔记(十三):JavaScript的继承方式
在Java.C++.C#等OO语言中,都支持两种继承方式:接口继承和实现继承.接口继承制继承方法签名,实现继承则继承实际的方法和属性.在SCMAScript中,由于函数没有签名,所以无法实现接口继承, ...
- Last_SQL_Errno: 1050
主库上create table,从库上存在. 报错信息如下所示: Last_SQL_Errno: 1050 Last_SQL_Error: ...