题目:详情看链接
链接 https://leetcode.cn/problems/exclusive-time-of-functions/
cache = []
for i in range(0,len(logs)-1):
a = logs[i].split(':')
b = logs[i+1].split(':')
if a[0] == b[0]:
if a[1] == b[1]:
res[int(a[0])] += int(b[2]) - int(a[2])
elif a[1] == 'start' and b[1] == 'end':
res[int(a[0])] += int(b[2]) - int(a[2]) + 1
elif a[1] == 'end' and b[1] == 'start':
res[int(a[0])] += int(b[2]) - int(a[2]) - 1
elif :
cache.append(int(a[0]))
# 后续不知道怎么写
class Solution:
def exclusiveTime(self, n: int, logs: List[str]) -> List[int]:
stack1 = [] # 函数
stack2 = [] # 开始时间
temp = 0 # 嵌套时间
stack3 = [0] # 记录是否刚进栈
res = [0] * n
for i in range(0,len(logs)):
a = logs[i].split(':')
if a[1] == 'start':
stack1.append(int(a[0]))
stack2.append(int(a[2]))
if len(stack3) == 0:
stack3.append(0)
else:
# 如果其前面还有函数没出栈,那么前面的函数对应的旗帜标为1
stack3[-1] = 1
stack3.append(0)
# 当函数都出栈后,记录嵌套时间重置为0
elif len(stack1) == 1:
fun = stack1.pop()
t1 = stack2.pop()
res[fun] = res[fun] + int(a[2])-t1+1-temp
temp = 0
else:
fun = stack1.pop()
t1 = stack2.pop()
# 一个函数所用的时间要减去其前面嵌套了的其他函数所用的时间
res[fun] = res[fun] + int(a[2]) - t1 + 1 -temp*stack3.pop()
# 更新temp
temp = temp + int(a[2]) - t1 + 1
return res
但这个代码过不了第九个实例:
[“0:start:0”,“0:start:1”,“0:start:2”,“0: end:3”,“0: end:4”,“0: end:5”]预期答案为6,但输出为4;错误的原因是,在多重嵌套时,temp会一直累加,但其实只需要减去上一层函数的时间
class Solution:
def exclusiveTime(self, n: int, logs: List[str]) -> List[int]:
stack1 = [] # 函数
stack2 = [] #开始时间
# temp = 0 # 嵌套时间
stack3 = [0] # 记录是否刚进栈
res = [0]*n
for i in range(0,len(logs)):
a = logs[i].split(':')
if a[1] == 'start':
stack1.append(int(a[0]))
stack2.append(int(a[2]))
# if len(stack3) == 0:
# stack3.append(0)
# else:
# stack3[-1] = 1
# stack3.append(0)
# 记录函数将要被其他函数占用的时间,不被占用即进去后就能立刻出即为0
stack3.append(0)
elif len(stack1) == 1:
fun = stack1.pop()
t1 = stack2.pop()
time = stack3.pop()
res[fun] = res[fun] + int(a[2]) - t1 + 1 - time
else:
fun = stack1.pop()
t1 = stack2.pop()
time = stack3.pop()
res[fun] = res[fun] + int(a[2]) - t1 + 1 - time
# 被占用的时间为int(a[2]) - t1 + 1
stack3[-1] = stack3[-1]+int(a[2]) - t1 + 1
return res
其实elif那一部分也可以省掉:
class Solution:
def exclusiveTime(self, n: int, logs: List[str]) -> List[int]:
stack1 = [] # 函数
stack2 = [] #开始时间
stack3 = [0] # 记录是否刚进栈
res = [0]*n
for i in range(0,len(logs)):
a = logs[i].split(':')
if a[1] == 'start':
stack1.append(int(a[0]))
stack2.append(int(a[2]))
stack3.append(0)
else:
fun = stack1.pop()
t1 = stack2.pop()
time = stack3.pop()
res[fun] = res[fun] + int(a[2]) - t1 + 1 - time
# 被占用的时间为int(a[2]) - t1 + 1
stack3[-1] = stack3[-1]+int(a[2]) - t1 + 1
return res
class Solution:
def exclusiveTime(self, n: int, logs: List[str]) -> List[int]:
ans = [0] * n
st = []
for log in logs:
idx, tp, timestamp = log.split(':')
idx, timestamp = int(idx), int(timestamp)
if tp[0] == 's':
if st:
ans[st[-1][0]] += timestamp - st[-1][1]
st[-1][1] = timestamp
st.append([idx, timestamp])
else:
i, t = st.pop()
ans[i] += timestamp - t + 1
if st:
st[-1][1] = timestamp + 1
return ans
复杂度分析
时间复杂度:O(n),其中 n 为全部日志 logs 的数量,n 条日志信息对应总共 n 次入栈和出栈操作。
空间复杂度:O(n),其中 n 为全部日志 logs 的数量,n 条日志信息对应n/2次入栈操作,最坏的情况下全部n/2条日志入栈后才会依次弹栈。
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/exclusive-time-of-functions/solution/han-shu-de-du-zhan-shi-jian-by-leetcode-d54e2/
# A code block
foo = 'bar';
# A code block
foo = 'bar';
# A code block
foo = 'bar';