news 2026/3/30 18:21:56

leetcode 3433

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
leetcode 3433

3433: 统计用户被提及情况

思路:排序+模拟

注意输入的 events 不保证是按时间顺序发生的,需要先排序。

按照时间戳 timestamp 从小到大排序,时间戳相同的,离线事件排在前面,因为题目要求「状态变更在所有相同时间发生的消息事件之前处理」。

然后模拟:

  • 离线事件:用一个数组 onlineT 记录用户下次在线的时间戳(60 秒后)。如果当前时间戳>= onlineT[i],则表示用户 i 已在线。
  • 消息事件:把相应用户的提及次数加一。
string& mention = e[2];

else{ // @id int idx=0; for(int i=0;i<mention.size();i++){ if(isdigit(mention[i])) idx=idx*10+(mention[i]-'0'); if(i+1==mention.size() || mention[i+1]==' '){ ans[idx]++; idx=0; } } }
  • isdigit(mention[i])是判断字符串mentioni个字符是不是十进制数字字符'0''9'
  • 1 <= numberOfUsers <= 100 因此要考虑可能出现形似id12 的情况idx=idx*10+(mention[i]-'0')
class Solution { public: vector<int> countMentions(int numberOfUsers, vector<vector<string>>& events) { vector<int> ans(numberOfUsers); vector<int> online_t(numberOfUsers,0); // 按照时间戳从小到大排序,时间戳相同的,离线事件排在前面 //两两比较,["MESSAGE","10","id1 id0"],["OFFLINE","11","0"] auto cmp=[](const vector<string>& lth,const vector<string>& rth){ int lth_tsp=stoi(lth[1]); //stoi适用于string转int int rth_tsp=stoi(rth[1]); if(lth_tsp!=rth_tsp) return lth_tsp<rth_tsp; else return lth[0]>rth[0]; // "OFFLINE" > "MESSAGE" 字典序 }; sort(events.begin(),events.end(),cmp); for(auto& e:events){ int curr_t=stoi(e[1]); // 当前时间 string& mention=e[2]; if(e[0][0]=='O'){ //离线事件 online_t[stoi(mention)]=curr_t+60; // 下次在线时间 } //消息事件 else if(mention[0]=='A'){ // @所有人 for(int i=0;i<numberOfUsers;i++) ans[i]++; } else if(mention[0]=='H'){ // @所有在线用户 for(int i=0;i<numberOfUsers;i++){ if(curr_t>=online_t[i]) ans[i]++; } } else{ // @id int idx=0; for(int i=0;i<mention.size();i++){ if(isdigit(mention[i])) idx=idx*10+(mention[i]-'0'); if(i+1==mention.size() || mention[i+1]==' '){ ans[idx]++; idx=0; } } } } return ans; } };
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/28 7:12:51

Apache Airflow自定义Docker镜像构建完全指南

Apache Airflow自定义Docker镜像构建完全指南 【免费下载链接】airflow Airflow 是一款用于管理复杂数据管道的开源平台&#xff0c;可以自动执行任务并监控其状态。高度可定制化、易于部署、支持多种任务类型、具有良好的可视化界面。灵活的工作流调度和管理系统&#xff0c;支…

作者头像 李华
网站建设 2026/3/29 23:44:37

商城小程序选型指南:技术架构、行业适配与功能考量

在那股数字化转型的浪潮形势之下呀&#xff0c;众多数量的企业&#xff0c;以及诸多的商家呢&#xff0c;都把目光放置到了商城小程序上面哦。商城小程序它身为一种轻量级的应用类型呀&#xff0c;是能够毫无缝隙地嵌入在微信、支付宝等之类的超级App里面的哟&#xff0c;进而成…

作者头像 李华
网站建设 2026/3/28 2:56:05

240亿参数重塑本地AI:Magistral Small 1.2开启多模态部署新纪元

240亿参数重塑本地AI&#xff1a;Magistral Small 1.2开启多模态部署新纪元 【免费下载链接】Magistral-Small-2509-GGUF 项目地址: https://ai.gitcode.com/hf_mirrors/unsloth/Magistral-Small-2509-GGUF 导语&#xff1a;Mistral AI推出的Magistral Small 1.2以240亿…

作者头像 李华
网站建设 2026/3/23 23:57:09

ComfyUI高级Redux控制:完整图像风格转换终极指南

ComfyUI高级Redux控制&#xff1a;完整图像风格转换终极指南 【免费下载链接】ComfyUI_AdvancedRefluxControl 项目地址: https://gitcode.com/gh_mirrors/co/ComfyUI_AdvancedRefluxControl 想要让Redux模型真正听从你的创意指令吗&#xff1f;ComfyUI_AdvancedReflux…

作者头像 李华
网站建设 2026/3/25 19:39:26

智能垃圾桶DIY指南:用超声波传感器打造自动感应开盖系统

智能垃圾桶DIY指南&#xff1a;用超声波传感器打造自动感应开盖系统 【免费下载链接】johnny-five JavaScript Robotics and IoT programming framework, developed at Bocoup. 项目地址: https://gitcode.com/gh_mirrors/jo/johnny-five 还在为每次扔垃圾都要手动开盖而…

作者头像 李华
网站建设 2026/3/26 11:04:07

Windows 11窗口直角化工具:一键告别圆角设计

Windows 11窗口直角化工具&#xff1a;一键告别圆角设计 【免费下载链接】Win11DisableRoundedCorners A simple utility that cold patches dwm (uDWM.dll) in order to disable window rounded corners in Windows 11 项目地址: https://gitcode.com/gh_mirrors/wi/Win11Di…

作者头像 李华