题目如下:

解题思路:本题和括号匹配问题有点像,用栈比较适合。一个元素入栈前,如果自己的状态是“start”,则直接入栈;如果是end则判断和栈顶的元素是否id相同并且状态是“start”,如果满足这两个条件,则说明这个元素和栈顶的元素是配对的,栈顶元素出栈。但是这个函数的的执行时间却不能简单的用end-start,因为这个函数里面可以还调用了其他函数,需要减去这些内部调用函数的执行时间,这里就需要引入一个新的变量来保存内部调用函数的执行时间。这个时间也很好求,函数完成配对后,栈顶元素出栈,只要把end-start累加到新的栈顶元素的内部调用函数执行时间即可。

代码如下:

class Solution(object):
def exclusiveTime(self, n, logs):
"""
:type n: int
:type logs: List[str]
:rtype: List[int]
"""
stack = []
res = [0] * n
for i in logs:
id,status,time = i.split(':')
if status == 'start' or len(stack) == 0 or stack[-1][0] != id:
stack.append([id,status,time,0])
else:
executetime = int(time) - int(stack[-1][2]) - int(stack[-1][3]) + 1
res[int(stack[-1][0])] += executetime
executerange = int(time) - int(stack[-1][2])
#ex = res[int(stack[-1][0])]
stack.pop(-1)
if len(stack) > 0:
stack[-1][3] += executerange + 1
return res

【leetcode】636. Exclusive Time of Functions的更多相关文章

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

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

  2. 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. 【Leetcode】Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  5. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  6. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  7. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  8. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  9. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

随机推荐

  1. What is httpcontext

    https://docs.microsoft.com/en-us/dotnet/api/system.web.httpcontext?view=netframework-4.8 Encapsulate ...

  2. 洛谷P1879 [USACO06NOV]玉米田Corn Fields(状压dp)

    洛谷P1879 [USACO06NOV]玉米田Corn Fields \(f[i][j]\) 表示前 \(i\) 行且第 \(i\) 行状态为 \(j\) 的方案总数.\(j\) 的大小为 \(0 \ ...

  3. 专人写接口+模型,专人写业务逻辑---interface_model -- business logical

    专人写接口+模型,专人写业务逻辑---interface_model -- business logical 0-控制台脚本重构为“面向接口编程”:1-仓库类通过__constru方法,来实现一处实例 ...

  4. Vagrant 入门 - 清理(teardown)

    原文地址 我们现在有一个功能齐全的虚拟机,可以用于基本 Web 开发.但如果现在需要更换设备,或者在另一个项目上工作,如何清理我们的开发环境? 借助 Vagrant,可以暂停(suspend),停止( ...

  5. excel wps access mysql数据表格的查询之路

    简直血崩,最近去做兼职,每天都有大量的表格数据要整理. 开始 还是 用 excel的用起来还算顺畅,慢慢慢慢的发现了各种弊端.大概类似于分组排序什么什么的好多啦~~~不过也确实是用了不到两个小时就能比 ...

  6. JS获取当前时间并格式化

    1.获取当前的日期时间 格式“yyyy-MM-dd HH:MM:SS”      function p(s) {        return s < 10 ? '0' + s : s;     ...

  7. 07 (H5*) js课程第8天 高阶函数、闭包、沙箱

    目录: 1:call和apply方法调用 2:bind方法复制 3:函数中的几个属性值 4:高阶函数,函数作为参数 5:高阶函数,函数作为返回值. 6:   作用域链,作用域,预解析 7:闭包--延长 ...

  8. Vue3.0响应式实现

    基于Proxy // 弱引用映射表 es6 防止对象不能被回收 let toProxy = new WeakMap(); // 原对象: 代理过得对象 let toRaw = new WeakMap( ...

  9. Python 学习笔记19 安装robot Framework

    因为项目组要做自动化测试,本人其实很希望能够使用 MStest + unit + C#来实现. 毕竟产品是基于.net 环境,并且使用C#环境开发的,适用性比较好,一些开发代码可以复用. 但是领导基于 ...

  10. Python之微信消息防撤回

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'jiangwenwen' import itchat from itchat. ...