当前位置:网站首页>leetcode:355. 设计推特
leetcode:355. 设计推特
2022-08-10 22:17:00 【OceanStar的学习笔记】
题目来源
题目描述

class Twitter {
public:
Twitter() {
}
void postTweet(int userId, int tweetId) {
}
vector<int> getNewsFeed(int userId) {
}
void follow(int followerId, int followeeId) {
}
void unfollow(int followerId, int followeeId) {
}
};
题目解析
实现一
思路
- 道题的主要难点在于实现 getNewsFeed() 函数,这个函数获取自己和好友的最近 10 条消息
- 需要两个映射:
- 映射:key = userId, value为当前用户关注的其他所有用户的ID的集合
- 这里只需要保证加入的用户ID唯一就可以了,可以用unordered_set来做
- 映射:key = userId, value为当前用户发送的所有博文
- 要想获得最近的消息,我们需要知道博文的时间发送先后顺序
- 因为tweetId不确定是不是自增的,所以我们定义一个cnt来模拟时间点,每发送一个消息,time就自增1,就知道哪个博文是先发的了
- 怎么将tweetId和cnt绑定起来呢?
- 我们可以定义一个结构体,也可以定义一个map(因为是一对一的),其key为time,value为对应tweetId。
- 为什么要key为time呢?
- 这样就会先根据time排序,如果time相等,就会根据tweetid排序
- 映射:key = userId, value为当前用户关注的其他所有用户的ID的集合
- 如何获取最近的10条消息呢?
- 维护一个大小为10的哈希表
- 然后遍历自己的好友,以及好友的所有消息(注意,先自己关注自己)
- 如果新遍历到的消息比map中最早的消息要晚,那么将这个消息加入,然后删除最早的那个消息(这里需要有序,所以用map而不是unordered_map)
class Twitter {
public:
Twitter() {
time = 0;
}
void postTweet(int userId, int tweetId) {
follow(userId, userId);
tweets[userId].insert({
time++, tweetId});
}
vector<int> getNewsFeed(int userId) {
vector<int> res;
map<int, int> top10;
for (auto id : friends[userId]) {
for (auto a : tweets[id]) {
top10.insert({
a.first, a.second});
if (top10.size() > 10) top10.erase(top10.begin());
}
}
for (auto a : top10) {
res.insert(res.begin(), a.second);
}
return res;
}
void follow(int followerId, int followeeId) {
friends[followerId].insert(followeeId);
}
void unfollow(int followerId, int followeeId) {
if (followerId != followeeId) {
friends[followerId].erase(followeeId);
}
}
private:
int time;
unordered_map<int, unordered_set<int>> friends;
unordered_map<int, map<int, int>> tweets;
};
下面这种方法和上面的基本一样,就是在保存用户所有消息的时候,用的是 vector<pair<int, int>>,这样可以用 priority_queue 来帮助找出最新 10 条消息
class Twitter {
int time;
std::unordered_map<int, std::unordered_set<int>> friends;
std::unordered_map<int, std::vector<std::pair<int, int>>> tweets;
public:
Twitter() {
time = 0;
}
void postTweet(int userId, int tweetId) {
follow(userId, userId);
tweets[userId].push_back({
++time, tweetId});
}
vector<int> getNewsFeed(int userId) {
std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<>> minPQ;
for(auto id : friends[userId]){
for(auto a : tweets[id]){
minPQ.push(a);
if (minPQ.size() > 10) minPQ.pop();
}
}
std::vector<int> vec;
while (!minPQ.empty()){
vec.insert(vec.begin(), minPQ.top().second);
minPQ.pop();
}
return vec;
}
void follow(int followerId, int followeeId) {
friends[followerId].insert(followeeId);
}
void unfollow(int followerId, int followeeId) {
if(followeeId != followerId){
friends[followerId].erase(followeeId);
}
}
};
实现二:哈希表 + 链表 + 优先队列

类似题目
边栏推荐
- OneNote 教程,如何在 OneNote 中整理笔记本?
- shell脚本
- JS use regular expressions in g model and non g difference
- 瑞幸咖啡第二季营收33亿:门店达7195家 更换CFO
- pytorch手撕CNN
- 美味的石井饭
- shell programming without interaction
- uni-app微信小程序——下拉多选框
- Fatal error: cstring: No such file or directory
- FPGA - Memory Resources of 7 Series FPGA Internal Structure -03- Built-in Error Correction Function
猜你喜欢

配电网络扩展规划:考虑使用概率性能源生产和消费概况的决策(Matlab代码实现)

ArcGIS中的坐标系统和投影变换

Qualcomm Platform Development Series Explanation (Application) Introduction to QCMAP Application Framework

元宇宙社交应用,靠什么吸引用户「为爱发电」?

QT笔记——QT工具uic,rcc,moc,qmake的使用和介绍

Distribution Network Expansion Planning: Consider Decisions Using Probabilistic Energy Production and Consumption Profiles (Matlab Code Implementation)

MySQL:MySQL的集群——主从复制的原理和配置

阿里云架构师金云龙:基于云XR平台的视觉计算应用部署

Shell programming specification and variables

边缘与云计算:哪种解决方案更适合您的连接设备?
随机推荐
边缘与云计算:哪种解决方案更适合您的连接设备?
String类的常用方法
瑞幸咖啡第二季营收33亿:门店达7195家 更换CFO
金山云CEO王育林离职:正值冲刺港股之际 雷军曾送金砖
“数据引擎”开启前装规模量产新赛道,「智协慧同」崭露头角
Shell 编程--Sed
学会开会|成为有连接感组织的重要技能
CIKM2022 | Sequence Recommendation Based on Bidirectional Transformers Contrastive Learning
电力系统潮流计算(牛顿-拉夫逊法、高斯-赛德尔法、快速解耦法)(Matlab代码实现)
IM 即时通讯开发如何设计图片文件的服务端存储架构
win系统下pytorch深度学习环境安装
JS中使用正则表达式g模式和非g模式的区别
Detailed installation steps and environment configuration of geemap
Black cat takes you to learn Makefile Part 11: When the header file a.h changes, how to recompile all the .c files that depend on the header file a.h
MySQL:MySQL的集群——主从复制的原理和配置
shell (text printing tool awk)
企业云存储日常运行维护实践经验分享
gcc492 compile `.rodata‘ can not be used when making a PIE object; recompile with -fPIE
OneNote 教程,如何在 OneNote 中整理笔记本?
测试4年感觉和1、2年时没什么不同?这和应届生有什么区别?