当前位置:网站首页>leetcode:636. 函数的独占时间【栈模拟】
leetcode:636. 函数的独占时间【栈模拟】
2022-08-07 15:10:00 【白速龙王的回眸】

分析
用st记录每个start的
如果碰上新的start,st最后的start要记录一次时长,加入新start
如果碰上新的end,记录时长,并更新pop之后的最后的start的时间
ac code
class Solution:
def exclusiveTime(self, n: int, logs: List[str]) -> List[int]:
ans = [0] * n
# 栈模拟
st = []
for log in logs:
idx, status, timestamp = log.split(':')
idx, timestamp = int(idx), int(timestamp)
if status == 'start':
# 中断了上一个start
if st:
ans[st[-1][0]] += timestamp - st[-1][1]
st.append([idx, timestamp])
else:
# 上一个start
iid, t = st.pop()
ans[iid] += timestamp - t + 1
if st:
# 更新上一个start
st[-1][1] = timestamp + 1
#print(ans)
return ans
总结
栈模拟
边栏推荐
猜你喜欢

Lianshengde W801 series 1-flash save data routine: save wifi distribution network information

深度之眼(二十一)——概率论

LeetCode高频题:dfs排列组合问题,剪枝策略,参观展厅总时间120分钟,请你给出所有可能的参观方案的个数

俩日总结(【18】、【19】)

RTT学习笔记7-中断管理

JWT的创建

RTT学习笔记10- 设备IPC 完成量-ringbufffer-workwueue

实验9 交换网络综合实验

RTT学习笔记12-串口外设和串口驱动框架

LeetCode Hot Question HOT 100 (7. The container that holds the most water)
随机推荐
RPG游戏地图场景管理维护(服务器)
C语言文件读写操作/标准IO
AdaptFormer学习笔记
LeetCode高频题:dfs排列组合问题,剪枝策略,参观展厅总时间120分钟,请你给出所有可能的参观方案的个数
JWT的创建
Open3D ICP精配准(点到面)
Open3D 点云变换(变换矩阵)
06 【Generic】
LeetCode HOT HOT 100 (5. Back to the longest text string)
Expert C Programming Chapter 8 Why Programmers Can't Tell the Difference Between Halloween and Christmas 8.3 Types Change While Waiting
小程序基础——全局配置_pages
微信小程序——小程序中计算属性的使用
[Principle of Database System] Chapter 4 Advanced Database Model: E/R Model and Its Design Rules and Constraints
sql,update偶发性报错,ERROR: invalid input syntax for integer: '0.00'?
俩日总结(【18】、【19】)
66:第五章:开发admin管理服务:19:开发【查看用户详情,接口】;
触摸屏如何利用无线PPI通信模块远程采集PLC数据?
Lianshengde W801 series 1-flash save data routine: save wifi distribution network information
Based on the FPGA VGA display color bar, characters, pictures
002_认识微服务