// 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. elasticsearch结构化查询过滤语句-----4

    1.之前三节讲述的都是索引结构及内容填充的部分,既然添加了数据那我们的目的无非就是增产改查crudp,我先来讲讲查询-----结构化查询 我们看上图截图两种方式: 1)第一种,在索引index5类型s ...

  2. ansible软件相关模块丶计划任务,剧本

    软件相关模块 yum rpm 和yum 的区别 rpm:redhat package manager yum可以解决依赖关系 yum 源配置 [epel] name=Extra Packages fo ...

  3. [转]C#利用委托跨线程更新UI数据

    在使用C#的过程中,难免会用到多线程,而用多线程之后,线程如何与界面交互则是一个非常头疼的问题.其实不仅仅是界面,一般情况下,我们往往需要获得线程的一些信息来确定线程的状态.比较好的方式是用委托实现, ...

  4. 夜色的 cocos2d-x 开发笔记 02

    本章我们让飞机发射子弹,因此我们要写这样一个方法 子弹资源:欢迎下载 很详细的注释吧,现在有几个地方报错,.h文件里面一定要先声明 这里是本章所有的新方法,你可以一次声明全部,嗯,还有个报错应该是我们 ...

  5. Cordova各个插件使用介绍系列(八)—$cordovaCamera筛选手机图库图片并显示

    原文档请看http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/ionic%E5%9B%BE%E7%89%87%E4%B8%8A%E4%B ...

  6. iptable防范ddos攻击

    Basic DoS Protection https://github.com/MPOS/php-mpos/wiki/Basic-DoS-Protection # Rule 1: Limit New ...

  7. $("#Upfile").MultiFile();

    Jquery的multifile 1.多文件上传: 2.如上几个验证不重复,和限制上传数量的验证显示的是英文,改成中文文本时,如果不用国标解码,到时候提示框会出现乱码现象.所以一般需要中文显示的时候, ...

  8. Ubuntu 系统安装

    1.首先下载一个Ubuntu系统文件,以.ios后缀结尾的系统镜像文件压缩包. 2,下载一个ultraiso软件,用于制作u盘启动盘 3,将电脑重启,进入BOIS界面,调整系统顺序, 将启动盘系统设置 ...

  9. 在微信小程序里自动获得当前手机所在的经纬度并转换成地址

    效果:我在手机上打开微信小程序,自动显示出我当前所在的地理位置: 具体步骤: 1. 使用微信jssdk提供的getLocation API拿到经纬度: 2. 调用高德地图的api使用经纬度去换取地址的 ...

  10. 打表格,字符串处理,POJ(2136)

    题目链接:http://poj.org/problem?id=2136 水题WA了半天,结果是数组开小了. #include <stdio.h> #include <string.h ...