LeetCode 636. [Python] Exclusive Time of Functions
原题链接
中等
作者:
徐辰潇
,
2021-09-22 04:59:20
,
所有人可见
,
阅读 465
class Solution:
def exclusiveTime(self, n: int, logs: List[str]) -> List[int]:
res = [0]*n
stack = []
task, action, time = logs[0].split(':')
task = int(task)
time = int(time)
stack.append(task)
for i in range(1,len(logs)):
prev_time = time
prev_action = action
task, action, time = logs[i].split(':')
task = int(task)
time = int(time)
if action == 'start':
if len(stack) != 0:
if prev_action == 'start':
res[stack[-1]] += (time - prev_time)
elif prev_action == 'end':
res[stack[-1]] += (time - prev_time-1)
stack.append(task)
else:
if prev_action == 'start':
res[stack[-1]] += (time - prev_time + 1)
elif prev_action == 'end':
res[stack[-1]] += (time - prev_time)
stack.pop()
return res