C++ | Leetcode C++题解之第355题设计推特

embedded/2025/2/22 23:02:44/

题目:

题解

class Twitter {struct Node {// 哈希表存储关注人的 Idunordered_set<int> followee;// 用链表存储 tweetIdlist<int> tweet;};// getNewsFeed 检索的推文的上限以及 tweetId 的时间戳int recentMax, time;// tweetId 对应发送的时间unordered_map<int, int> tweetTime;// 每个用户存储的信息unordered_map<int, Node> user;
public:Twitter() {time = 0;recentMax = 10;user.clear();}// 初始化void init(int userId) {user[userId].followee.clear();user[userId].tweet.clear();}void postTweet(int userId, int tweetId) {if (user.find(userId) == user.end()) {init(userId);}// 达到限制,剔除链表末尾元素if (user[userId].tweet.size() == recentMax) {user[userId].tweet.pop_back();}user[userId].tweet.push_front(tweetId);tweetTime[tweetId] = ++time;}vector<int> getNewsFeed(int userId) {vector<int> ans; ans.clear();for (list<int>::iterator it = user[userId].tweet.begin(); it != user[userId].tweet.end(); ++it) {ans.emplace_back(*it);}for (int followeeId: user[userId].followee) {if (followeeId == userId) continue; // 可能出现自己关注自己的情况vector<int> res; res.clear();list<int>::iterator it = user[followeeId].tweet.begin();int i = 0;// 线性归并while (i < (int)ans.size() && it != user[followeeId].tweet.end()) {if (tweetTime[(*it)] > tweetTime[ans[i]]) {res.emplace_back(*it);++it;} else {res.emplace_back(ans[i]);++i;}// 已经找到这两个链表合起来后最近的 recentMax 条推文if ((int)res.size() == recentMax) break;}for (; i < (int)ans.size() && (int)res.size() < recentMax; ++i) res.emplace_back(ans[i]);for (; it != user[followeeId].tweet.end() && (int)res.size() < recentMax; ++it) res.emplace_back(*it);ans.assign(res.begin(),res.end());}return ans;}void follow(int followerId, int followeeId) {if (user.find(followerId) == user.end()) {init(followerId);}if (user.find(followeeId) == user.end()) {init(followeeId);}user[followerId].followee.insert(followeeId);}void unfollow(int followerId, int followeeId) {user[followerId].followee.erase(followeeId);}
};

http://www.ppmy.cn/embedded/104875.html

相关文章

微服务--认识微服务

微服务架构的演变 1. 单体架构&#xff08;Monolithic&#xff09; 阶段描述&#xff1a;在单体应用时代&#xff0c;整个应用程序被设计为一个项目&#xff0c;并在一个进程内运行。这种架构方式开发简单&#xff0c;便于集中管理&#xff0c;但随着应用的复杂化&#xff0c…

DOM树和CSS树解读

DOM树&#xff08;Document Object Model Tree&#xff09;和CSS树&#xff08;CSSOM Tree, CSS Object Model Tree&#xff09;是Web浏览器用来表示和渲染网页的重要数据结构。理解它们有助于掌握网页渲染的原理&#xff0c;从而能够优化网页性能。 1. DOM树&#xff08;Docu…

服务器数据恢复—如何应对双循环RAID5阵列的数据丢失问题?

服务器存储数据恢复环境&#xff1a; 一台存储中有一组由7块硬盘组建的RAID5阵列&#xff0c;存储中还有另外3块盘是raid中掉线的硬盘&#xff08;硬盘掉线了&#xff0c;管理员只是添加一块的新的硬盘做rebuild&#xff0c;并没有将掉线的硬盘拔掉&#xff09;。整个RAID5阵列…

四种NAT类型

1.全锥型NAT&#xff08;Full Cone NAT&#xff09; 全锥型NAT&#xff08;Full Cone NAT&#xff09;&#xff0c;又称为一对一NAT&#xff0c;是四种主要NAT类型之一&#xff0c;用于实现私有网络与公共网络之间的IP地址转换和端口转换。在全锥型NAT中&#xff0c;一个内部I…

【Python机器学习】NLP词频背后的含义——隐性狄利克雷分布(LDiA)

目录 LDiA思想 基于LDiA主题模型的短消息语义分析 LDiALDA垃圾消息过滤器 更公平的对比&#xff1a;32个LDiA主题 对于大多数主题建模、语义搜索或基于内容的推荐引擎来说&#xff0c;LSA应该是首选方法。它的数学机理直观、有效&#xff0c;它会产生一个线性变换&#xff…

前端小白操作指南:如何删除项目中 pre-commit 的提交限制?

最近在维护一个项目时&#xff0c;我遇到了一个问题&#xff1a;项目中设置了pre-commit限制&#xff0c;每次提交代码前都需要通过一系列的检查。这虽然能提高代码质量&#xff0c;但在一些紧急情况下或者进行大量小修改时&#xff0c;这些限制反而显得有些繁琐和费时。我开始…

【python因果推断库3】使用 CausalPy 进行贝叶斯geolift 分析

目录 导入数据 丹麦的销售额是否有地理提升&#xff08;GeoLift&#xff09;&#xff1f; 结果 本笔记本介绍如何使用 CausalPy 的贝叶斯{术语}合成控制功能来评估“地理提升”&#xff08;GeoLift&#xff09;。我们的假设情景如下&#xff1a; 你是一家在欧洲运营的公司的…

zabbix和prometheus介绍;云原生

监控 Prometheus和Zabbix作为两种流行的监控系统&#xff0c;它们在多个方面存在显著的差异。以下是对两者区别的详细分析&#xff1a; 一、数据模型与采集方式 Prometheus&#xff1a; 数据模型&#xff1a;基于度量指标的模型&#xff0c;支持多维度数据模型&#xff0c;每…