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

devtools/2024/9/24 13:57:21/

题目:

题解

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/devtools/99697.html

相关文章

SpringBean

1. 什么是Spring Bean 定义: Spring Bean是由Spring IoC容器管理的对象。是应用程序的核心组成部分&#xff0c;通常是服务、DAO、控制器等。 2. Bean的定义方式 XML配置: 通过XML文件定义Bean。 <beans xmlns"http://www.springframework.org/schema/beans"x…

Swift 中的影像魔术:Core Video 的高级应用

标题&#xff1a;Swift 中的影像魔术&#xff1a;Core Video 的高级应用 在 Swift 开发中&#xff0c;Core Video 是 Apple 提供的一个强大的框架&#xff0c;用于处理高质量的视频内容。从实时视频滤镜到高级图像处理&#xff0c;Core Video 为开发者提供了丰富的 API 来实现…

链动 2+1 模式小程序 AI 智能名片商城源码培训邀约策略研究

摘要&#xff1a;本文深入剖析链动 21 模式小程序 AI 智能名片商城源码的培训邀约策略&#xff0c;从该源码的价值出发&#xff0c;阐述邀约的重要性&#xff0c;并详细介绍具体的邀约策略&#xff0c;旨在为相关培训活动提供切实可行的指导&#xff0c;提高邀约成功率&#xf…

Git 版本控制操作

1. 版本回退 Git 能够管理⽂件的历史版本&#xff0c;这是版本控制器重要的能⼒。如果有⼀天你发现之前前的⼯作做的出现了很⼤的问题&#xff0c;需要在某个特定的历史版本重新开始&#xff0c;这个时候&#xff0c;就需要版本回退的功能了。 执⾏ git reset 命令⽤于回退版…

6.2 频率域滤波之高通滤波

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言1. 理论基础2. 示例分析2.1 示例代码2.2 示例效果分析 前言 上一章我们讲到频率低通滤波&#xff0c;简而言之就是让图像的低频信号通过&#xff0c;过滤或者衰…

微信小程序:点击事件(bindtap)传递参数

小程序在组件上绑定事件后&#xff0c;传递参数的方式不同于前端其他场景中直接加参数的方式&#xff0c;小程序在参数的传递时&#xff0c;采用事件对象的自定义属性的方式&#xff0c;具体实现如下&#xff1a; wxml&#xff1a; <view bindtap"goIndex" data…

True XML cookbook

打开题目 看到登录口 随便输入admin&#xff0c;123456&#xff0c;然后抓包试一下 先按原来那道题的payload进行测试&#xff0c;payload和结果如下&#xff1a; <?xml version"1.0" ?> <!DOCTYPE llw [ <!ENTITY file SYSTEM "file:///flag&…

pycharm修改文件大小限制

场景&#xff1a; 方法&#xff1a; 打开pycharm 安装目录下的idea.properties 增加配置项&#xff1a;idea.max.intellisense.filesize99999