【leetcode】636. Exclusive Time of Functions
题目如下:

解题思路:本题和括号匹配问题有点像,用栈比较适合。一个元素入栈前,如果自己的状态是“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的更多相关文章
- 【LeetCode】636. Exclusive Time of Functions 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【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 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
随机推荐
- windowns环境下mysql 安装教程
windowns环境下mysql 安装教程 一:这里以绿色版安装为例(解压就可以使用) 下载地址: 下载页面:https://dev.mysql.com/downloads/mysql/ 2:点击 ...
- leetcode 171. Excel表列序号(python)
给定一个Excel表格中的列名称,返回其相应的列序号. 例如, A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ...
- PHP 调试 - 方式
之前学 Java 的时候,一直使用 IDE 的 console 控制台进行调试.后来搞 PHP 后,习惯在代码里面 echo 和 exit,然后在浏览器刷新看效果,把单步调试.变量值查看等常用的调试方 ...
- Vagrant 手册之 Multi-machine 多机器
原文地址 Vagrant 可以在一个 Vagrantfile 中定义并控制多个虚拟机.这就是"multi-machine"环境. 这些机器可以协同工作或互相关联.multi-mac ...
- 给url添加时间戳,解决浏览器缓存
//解决浏览器缓存function timestamp(url){ // var getTimestamp=Math.random(); var getTimestamp=new Date().get ...
- [LeetCode] 477. Total Hamming Distance(位操作)
传送门 Description The Hamming distance between two integers is the number of positions at which the co ...
- Lambda拉姆达表达式
拉姆达表达式常用于委托,也就是说拉姆达表达式是匿名函数,简单点就是函数. a => a.Equals("string"); //原形为: (a) => { return ...
- MySQL-极恶安装
1.官网下载地址:https://dev.mysql.com/downloads/mysql/ 2.安装包下载后解压,并创建my.ini配置文件 内容如下,注意两个第三个#:MySQL的安装目录,第四 ...
- 标准标签库JSTL(JSP Standard Tag Library)
1, 核心标签(最常用, 最重要的) 表达式控制标签 out 输出常量 value---直接赋值 输出变量 default---默认值 escapeXml---控制转义字符(默认为true, 如果需要 ...
- Spring切面编程之AOP
AOP 是OOP 的延续,是Aspect Oriented Programming 的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种 ...