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

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

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

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

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

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

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

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

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

  5. 【leetcode】636. Exclusive Time of Functions

    题目如下: 解题思路:本题和括号匹配问题有点像,用栈比较适合.一个元素入栈前,如果自己的状态是“start”,则直接入栈:如果是end则判断和栈顶的元素是否id相同并且状态是“start”,如果满足这 ...

  6. Leetcode 之 Exclusive Time of Functions

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

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

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

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

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

  9. Exclusive Time of Functions

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

随机推荐

  1. java基础:JDK环境安装

    根据操作系统位数(32/64,一般64位向下兼容),项目要求版本,下载对应JDK安装包 配置环境变量 JAVA_HOME C:\Program Files\Java\jdk1.7.0_80 PATH ...

  2. VirtualBox中linux虚拟机和主机间的共享文件设置

    设置共享文件路径 点击虚拟机 设置-->选择 共享文件夹 (图1 设置共享文件夹) 设置共享文件夹路径 1 选择路径 2 填写自定义的共享名称(在后面需要与挂载路径相对应) 3 设置自动挂载/固 ...

  3. Python3爬虫04(其他例子,如处理获取网页的内容)

    #!/usr/bin/env python# -*- coding:utf-8 -*- import osimport reimport requestsfrom bs4 import Navigab ...

  4. 2009中国IT界名人

    丁  磊:网易创始人 李  想:泡泡网                   辛佳雨:代码中国创始人 张朝阳:搜狐创业者之一 王志东:新浪网创业者之一 陈天桥:盛大网络创始人,中国互动娱乐业第一人 周鸿 ...

  5. linux基础命令-chgrp/chown/chomd

    chgrp 改变所属用户组 要被改变的组名必须要在/etc/group文件内存在才行: chgrp [-R]   dirname/filename -R:进行递归的持续更改,连同子目录下的所有文件.目 ...

  6. php中的curl_multi的应用(php多进程)

    相信许多人对PHP手册中语焉不详的curl_multi一族的函数头疼不已,它们文档少,给的例子 更是简单的让你无从借鉴,我也曾经找了许多网页,都没见一个完整的应用例子. curl_multi_add_ ...

  7. Codeforces Round #261 (Div. 2) - E (459E)

    题目连接:http://codeforces.com/contest/459/problem/E 题目大意:给定一张有向图,无自环无重边,每条边有一个边权,求最长严格上升路径长度.(1≤n,m≤3 * ...

  8. MySQL入门很简单: 9 插入 更新与删除数据

    1. 插入数据:INSERT 1)为表的所有字段插入数据 第一种: 不指定具体的字段名 INSERT INTO 表名 VALUES(值1,值2,...,值n): 第二种:INSERT语句中列出所有字段 ...

  9. ABAP的include关键字,Java的import, C的include和C4C ABSL 的import比较

    ABAP 使用关键字重复引入一个include program,会报syntax error: 原因是因为ABAP对include这个关键字的实现和其他编程语言有点不一样,在激活时简单地把被inclu ...

  10. 【转载】#335 - Accessing a Derived Class Using a Base Class Variable

    You can use a variable whose type is a base class to reference instances of a derived class. However ...